-2

I am trying to elicit two responses depending on user input, and can't get it to work. It just keeps printing "Correct, seems you're smarter than I thought...". Any help would be much appreciated, thank you

print ("Welcome to UTOPIA")


river= ""
while not river:
    river = input ("\n\n\n\nYou come across a raging river, what do you do? ")

if river == "swim" or "swim through it":
    print ("Correct, seems you're smarter than I thought...")

elif river == "walk through it" or "walk through":
    print ("You cant walk through such a big river... silly!")

else:
    print ("Well, sensible suggestions now...")
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • your `if river == "swim" or "swim through it":` is always true, due to the way `or` works in python. – Roars Dec 05 '17 at 14:50
  • you need to do `or river == "swim through it"` – Roars Dec 05 '17 at 14:51
  • Possible duplicate of [Comparing a string to multiple items in Python](https://stackoverflow.com/questions/6838238/comparing-a-string-to-multiple-items-in-python) – mkrieger1 Dec 05 '17 at 14:52
  • note that `if "swim through it":` will always be seen as true as it is not an empty string. – Roars Dec 05 '17 at 14:53
  • Better duplicate: https://stackoverflow.com/questions/15112125/how-do-i-test-multiple-variables-against-a-value – mkrieger1 Dec 05 '17 at 14:53

3 Answers3

2

The issue has to do with your if statements. An or will not automatically look at the last variable used so it must be specified again. If "some string" will always evaluate to true as long as the string isn't empty.

print ("Welcome to UTOPIA")

river= ""
while not river:
    river = input ("\n\n\n\nYou come across a raging river, what do you do? ")

if river == "swim" or river == "swim through it": #Notice the change
    print ("Correct, seems you're smarter than I thought...")

elif river == "walk through it" or river == "walk through": #Notice the change
    print ("You cant walk through such a big river... silly!")

else:
    print ("Well, sensible suggestions now...")
NendoTaka
  • 1,224
  • 8
  • 14
0

Because you're doing

if river == "swim" or "swim through it":
    # ...

which is not correct code. Essentially you're saying "If river is swim or string "swim through it" " which doesn't make sense.

You're looking for

if river == "swim" or river == "swim through it"
Treyten Carey
  • 641
  • 7
  • 17
0

All non-empty strings are (unfortunately) True in Python.

That means that this will always be True:

if something or "string":

Because or "string" will always be True.

Alexander
  • 9,737
  • 4
  • 53
  • 59