-1

I was working on a gladiator text adventure type game to help learn python. The only programming experience I have had is some basic c++ a few years ago. The code is working like expected for both yes and no but when you enter something else it is still executing the body even though the test condition is not true so it still just displays "You chose" and whatever you chose no matter what you typed. Through some research I seen someone on here say empty strings are always evaluated to true? If that is the case how do I get around this. I was thinking I could make a variable equal to each string I am testing for but is their another way to go about this?

 #Intro
print("Please enter your name Brave Challenger")
name = input("")
print (name,"Are you ready to battle")

#Selecting yes or no
print ("Please enter yes or no")

#storing  yes or no or other inside of variable
selection = input()

#Displaying whether selection is yes, no, or other
if selection == "yes" or "YES" or "Yes":
    print ("You chose", selection)
elif selection == "no" or "NO" or "No":
    print ("You chose", selection)
else: 
    print("You must choose Yes or No")
iz_
  • 15,923
  • 3
  • 25
  • 40
  • the condition for that `elif` is `(selection == "no") or ("NO") or ("No")`, and `"NO"` evaluates to `True` since it's a non-empty string. A cleaner way to do this would be `elif selection.lower() == "no":` – Henry Woody Jan 28 '19 at 00:04
  • The syntax you use is correct and Python will do exactly what you tell it to but it doesn't do what you're hoping it might do. Before the first `if` statement, try printing `selection == "yes" or "YES" or "Yes"` and it will always print True because testing "YES" will always return True – DisappointedByUnaccountableMod Jan 28 '19 at 00:05
  • The way to debug this sort of problem is simple: print the value of each expression you are testing in the if/elif statements. – DisappointedByUnaccountableMod Jan 28 '19 at 00:09

2 Answers2

0

Make this

if selection == "yes" or "YES" or "Yes":
    print ("You chose", selection)
elif selection == "no" or "NO" or "No":
    print ("You chose", selection)
else: 
    print("You must choose Yes or No")

this

if selection == "yes" or selection == "YES" or selection == "Yes":
    print ("You chose", selection)
elif selection == "no" or selection == "NO" or selection == "No":
    print ("You chose", selection)
else: 
    print("You must choose Yes or No")
duncster94
  • 570
  • 6
  • 23
0

The issue is almost certainly in lines like this: selection == "yes" or "YES" or "Yes".

You can either expand it:

selection == "yes" or selection == "YES" or selection == "Yes"

Or you could do this:

if selection in ["yes", "YES", "Yes"]

Alex
  • 2,270
  • 3
  • 33
  • 65