Python Forum
Using Input() in If...Elif Statements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Input() in If...Elif Statements
#1
How would I make it so the user only has to type heal once?

if (input() == "hunt"):
    print("You encountered a slime! What would you like to do?")

elif (input() == "heal"):
    print("Your health is restored!")
However, the user has to type something that is not hunt, then type heal, and then it runs the elif statement. I know it is because it first asks for input, and it does not see hunt, so it asks for input again, and then if the second input is heal it outputs the print statement.
Reply
#2
Just use input() once and assign what you entered to a variable.
Use that variable in your if conditions.
Reply
#3
I tried that, but then the variable is set to either hunt or fight, and so it defaults to the first response, and I want to let the user hunt or heal more than once.

I tried that, but now the elif statement is not running. It is going straight to the else statement.

decision = input("What would you like to do?")

if (decision == "hunt"):
    print("You encountered a slime! What would you like to do?")

elif (decision == "heal"):
    print("Your health is restored!")

else:
    print("That is not an option. Would you like to hunt or heal?")
Reply
#4
You need to put your code into a loop if you want it to be repeated. I´d suggest:
while True:
    decision = input("Enter command: (quit to exit)")
    if decision == .....
        do code
    if decision == .....
        do code
    if decision == "quit":
        break
Reply
#5
while (True):
    decision = input("What would you like to do?")
 
    if (decision == "hunt"):
        print("You encountered a slime! What would you like to do?")
 
    elif (decision == "heal"):
        print("Your health is restored!")
 
    else:
        print("That is not an option. Would you like to hunt or heal?")
I did put it in a while loop. I should have included that earlier.
Reply
#6
Fine.
Be advised that you don´t need to put brackets around every condition,
escpecially not around python keywords like True or False.
So all brackets in your code can be removed.
Reply
#7
Sorry, I am used to using JavaScript. Also, I did not know that. Do you have any advice on how I can solve my problem?
Reply
#8
Oh, i thought your problem is solved.
If there is still one, please describe what you want to do and what your code does not do.
Reply
#9
Well, what I want to happen is, when the user types hunt, they encounter a slime. When they type heal, they restore their health; the print statements represent those actions. I have the while loop so that they can heal or hunt more than once. If they do not type either hunt or heal, it will do the else statement, then they can type hunt or heal. However, what is happening is, when I store the input in a variable, and I check if that variable equals either hunt or heal, they can type hunt and encounter a slime. If they type heal, however, it goes straight to the else statement, and seems to skip the elif statement. Any advice?
Reply
#10
One possibility is separate validation of input from action triggered from input:

# define validating function which returns either 'hunt' or 'heal'
# define dictionary where keys are valid inputs and values are desired outputs
# then just: print(dictionary_name[validated_user_input])
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,282 Jul-28-2019, 05:52 PM
Last Post: ichabod801
  an input error or elif error , how can i fix this the_fire_pharaoh 1 2,383 Dec-15-2018, 09:47 PM
Last Post: Gribouillis
  Unexpected input within an if/elif statement fier259 2 2,863 May-12-2018, 07:41 AM
Last Post: buran
  input issue elif jge047 2 3,230 Nov-23-2017, 06:29 AM
Last Post: zykbee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020