0

When I type no into the input on the terminal. It goes through the if choice == "yes" part. I want it to go through the else. Please help.

choice=raw_input("Will you help us? Yes or no?")

if choice == "yes" or "Yes":
    print "Yeah! You are a hero!"
    name = raw_input("What is your name?")
    print "Alright, " + str(name) + " ,let's go choose a weapon from the blacksmith."

else:
    print "You're a coward. :("
    quit()

What's wrong?

Mariba
  • 39
  • 8

2 Answers2

0
choice=raw_input("Will you help us? Yes or no?")

if choice == "yes" or choice == "Yes":
    print "Yeah! You are a hero!"
    name = raw_input("What is your name?")
    print "Alright, " + str(name) + " ,let's go choose a weapon from the blacksmith."

else:
    print "You're a coward. :("
    quit()

The above is the correct format. You did not have the logic set up correctly. Note the following:

a = 1
if a == 2 or 3 :
    print 'OK'

It prints 'OK'. Why?

The reason is that python values are evaluated in a left to right fashion. If any value is true then that value is returned. However if all values are false then the last value is returned, in your case 'Yes'. This is what is causing you problems as far as I understand it. You need basically two 'or' conditions, not one....

Woody Pride
  • 13,539
  • 9
  • 48
  • 62
0

The bug is in this line of code:

if choice == "yes" or "Yes":

Python sees this as an "or" of two conditions:

if (choice == "yes") or ("Yes"):

Which is same as:

if (choice == "yes") or True:

because a non-empty string is always True. And this finally reduces to:

if True:

as "or"ing with True always evaluates to True.

This would give you the desired result:

if choice == "yes" or choice == "Yes":

However, that is considered C-style and the pythonic way of comparing multiple values is:

if choice in ("yes", "Yes"):

But in this case, you just want to do a case-insensitive match. So the right way to do that would be:

if choice.lower() == "yes":

And this would even handle odd capitalization in inputs like "yEs" or "YEs".

Saish
  • 511
  • 3
  • 11