1

I have this function:

def abandoned_station():
print """
.....
""" 

    next = raw_input(prompt)

    if next == "store room" or "Store Room" or "Store room":
        store_room()
    elif next == "control room" or "Control Room" or "Control room":
        control_room()
    elif next == "Look" or "look":
        abandoned_station
    else:
        print "Sorry, I do not understand that command."

But when I input 'Control Room' at the prompt, it goes to the store_room function. If I enter anything else, it goes back to the original abandoned_station function - it doesn't even say it doesn't understand.

Have I created a loop here that I have missed?

Thanks in advance.

Silas Ray
  • 25,682
  • 5
  • 48
  • 63
JoelSolo
  • 13
  • 3

1 Answers1

0

If you do your comparisons like that:

if next == "store room" or "Store Room" or "Store room":

What it really means is

if (next == "store room") or ("Store Room") or ("Store room"):

That means, Pythons dynamic nature evaluates the boolean of a string, which is True unless it is empty. You have to compare every clause with next:

if (next == "store room") or (next  == "Store Room") or (next == "Store room"):

To quote the Python documentation:

In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true. (See the nonzero() special method for a way to change this.)

Source: Python doc

jcklie
  • 4,054
  • 3
  • 24
  • 42