简体   繁体   中英

Still new to python. So confused right now. How do I make this program work?

So I'm making a little game to teach myself how python works, and it's been okay up to this point, but now I'm completely stuck. I'm sure the logic and formatting of the if and while statement are all wrong, so I'd be happy if people could point me in the right direction - again, REALLY new to programming in general. I'll paste the code, then explain what I'm trying to make it do:

import random
import time

print("Welcome to the Dojo!")
print("You have three opponents; they are ready...")
print("Are you?")
print("*To view the rules, type 'rules' ")
print("*To view commands, type 'commands' ")
print("*To begin, type 'start' ")

while True:
    userInput = (input())
    # Rules
    if userInput == "rules":
        print("The rules in this Dojo are simple. Kill your opponent! Fight to the death! Show no mercy!")
        print("Each opponent gets progressively more difficult, whether it be in terms of health or damage.")
        print("To attack, type 'attack'")
        print("May the better (luckier) warrior win")
    # Commands - to be added
    elif userInput == "commands":
        print("Commands will be added soon!")
    # Start
    elif userInput == "start":
            damage = random.randint(1, 50)
            userHealth = int(100)
            opponentHealth = int(100)
            print("Your first opponent is Larry Schmidt. Don't sweat it, he'll be a piece of cake.")
            time.sleep(3)
            print("The battle will begin in")
            time.sleep(1)
            print("5")
            time.sleep(1)
            print("4")
            time.sleep(1)
            print("3")
            time.sleep(1)
            print("2")
            time.sleep(1)
            print("1")
            time.sleep(1)
            print("Fight!")
            if userInput == "attack":
                int(userHealth - damage)
                print("You did %(damage) to Larry!")

    # Invalid response
    else:
        print("Enter a valid response, young grasshopper.")
        if userInput == "start" is True:
            continue

If you run the program for yourself, everything is okay until you reach the "5, 4, 3, 2, 1, Fight!" part. Then when you type "attack," it gives you "Enter a valid response, young grasshopper." I understand why this is happening, because "attack" doesn't fall under "rules", "commands", or "start". I just don't get how I'm supposed to format it so that after I enter "attack", it actually proceeds and outputs the damage done, etc. I apologize if this is hard to understand, but I think if you run it for yourself, you'll understand what I'm having problems with. Honestly, something tells me I've messed up the if and while statements up entirely, but hey, I'm learning and having fun :P.

Thanks for any help.

You aren't asking for more input. It's guaranteed that the userInput will still be "start" at that point.

Your code would benefit greatly from the use of functions . You could really organize the code better that way.

def intro():
    print("Welcome to the Dojo!")
    print("You have three opponents; they are ready...")
    print("Are you?")
    print("*To view the rules, type 'rules' ")
    print("*To view commands, type 'commands' ")
    print("*To begin, type 'start' ")

def get_user_input():
    user_input = (input())
    while user_input not in ['rules', 'commands', 'start']:
        print("valid commands are rules, commands and start")
        user_input = (input())
    return user_input


intro()
returned_user_input = get_user_input()

There are a lot of ways to do these things, and you'll get better with time. Mainly note that user_input only gets updated when you do this:

user_input = (input())

Which in your code was only done once.

The code you have in the "start" branch of the if conditional will only run once, and userInput will still be "start" at that point. The solution, wrap it in a loop. Maybe something like this:

elif userInput == "start":
    userHealth = 100
    opponentHealth = 100

    print("Your first opponent is Larry Schmidt. Don't sweat it, he'll be a piece of cake.")
    time.sleep(3)
    print("The battle will begin in")
    time.sleep(1)
    print("5")
    time.sleep(1)
    print("4")
    time.sleep(1)
    print("3")
    time.sleep(1)
    print("2")
    time.sleep(1)
    print("1")
    time.sleep(1)
    print("Fight!")

    while opponentHealth > 0 and userHealth > 0:
        userInput = input()

        if userInput == "attack":
            damage = random.randint(1, 50)
            opponentHealth = opponentHealth - damage
            print("You did %d to Larry!" % damage)

The while loop will loop until the opponent has been defeated or until the user is defeated, but at the moment, there's no damage done to the user. I'll leave that part to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM