0

Oh, man! Sorry, I'm quite new to Python. I was just being an idiot. So don't answer this.

in Python I tried to use the if/else function, but it always chose the "if" option. I use Python 2 (IDLE). How can you help? Here is my code:

if answer=="Yes" or "yes":
  upload_name=raw_input("What do you want to change it to? ")
elif answer=="No" or "no":
  print "Okay. Your name will remain as " + str(real_name) + "."

1 Answers1

1

if answer=="Yes" or answer== "yes":

elif answer=="No" or answer== "no":

Or just use if answer.lower() == "yes"

or "yes" always evaluates to True once the string is a non empty string, you are not comparing to answer, you are testing bool("yes").

In [1]: bool("yes")
Out[1]: True

In your case I would just call lower on the string but if you have multiple values to test against using in can be a nice way to do it:

if answer in {"Yes","yes"}:
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321