Home > OS >  Why the if-else statement is not giving me the right prompt, even when I put the right information i
Why the if-else statement is not giving me the right prompt, even when I put the right information i

Time:10-17

I am trying to do some questions through what-if statements in phyton. Basically, depending on what you answer you should get the next question

match_flashlight = input("You are walking through a dark forest and find two items: a MATCH and a FLASHLIGHT. Which one do you want to pick up?")

if match_flashlight.lower() == "match":
   run_hide = input("You pick up the match and strike it, and for an instant, the forest around you is illuminated. You see a large grizzly bear, and then the match burns out. Do you want to RUN, or HIDE behind a tree?")  

else: 
    print("You put an invalid answer")

Now, if I enter match, it should give the question that is inside the variable called run_hide, but it is just giving me the printing the else statement. Can someone explain to me why this is happening?

CodePudding user response:

I typed Matchand it works just fine as you expected. What input are you giving? Btw, if you added any space before or after the match it won't work. You may find the strip() method useful in this case. It returns a new string with the spaces trimmed from either side (if any). Here's how you do it:

match_flashlight = input("You are walking through a dark forest and find two items: a MATCH and a FLASHLIGHT. Which one do you want to pick up?").strip()

CodePudding user response:

This code looks like part of the whole code. And it works for me too. It seems "You put an invalid answer" is somewhere else. How about looking for another part?

  • Related