dubble_oh_nexus
dubble_oh_nexus

Reputation: 55

Program in python printing out the wrong thing no matter what

when running this code:

print("You wake up with a jolt. \"Damn it's dark in here\"")
print("To the north is the cell door, i think")
print("To the west is a cot, maybe")
print("To the east is a toilet")
print("and to the south is a window, it looks like night time")

cellDoor = False
toilet = False
cot = False
window = False
sawClock = False

while(cellDoor == False or toilet == False or cot == False or window == False):
    insideCell = raw_input("Where should i go?").lower()

    if insideCell == "north" or "go north" or "walk north":
        cellDoor = True
        print("I can vaugly make out where the walkway ends and drops to the prison     floor")
    elif insideCell == "east" or "go east" or "walk east":
        toilet = True
        print("Ugh, this smells used")
    elif insideCell == "west" or "go west" or "walk west":
        cot = True
        print("<sarcasm> Oh boy, that looks comfortable. I cannot wait to sleep on that      </sarcasm>")
    elif insideCell == "south" or "go south" or "walk south":
        window = True
        print("It's night time, I wonder when specifically")
        print("I remember a clock on the western wall, i should look there")
        while(sawClock == False):
            findClock = raw_input("Look at the clock").lower()
            if findClock == "look west" or "turn west":
                print("It's about 23:00, i should get some sleep, after i finsih exploring, of course")
                sawClock = True

print("Let's get some sleep now")

when ever i type "go south" or anything, it prints what should ONLY happen after typing "go north" and the like. I need this to not happen, I need each specific thing to happen ONLY when the proper command is issued.

the output currently is:

You wake up with a jolt. "Damn it's dark in here"
To the north is the cell door, i think
To the west is a cot, maybe
To the east is a toilet
and to the south is a window, it looks like night time
Where should i go?south
I can vaugly make out where the walkway ends and drops to the prison floor
Where should i go?north
I can vaugly make out where the walkway ends and drops to the prison floor
Where should i go?west
I can vaugly make out where the walkway ends and drops to the prison floor
Where should i go?east
I can vaugly make out where the walkway ends and drops to the prison floor
Where should i go?

Upvotes: 0

Views: 133

Answers (3)

drodri
drodri

Reputation: 5972

In the expression:

if insideCell == "north" or "go north" or "walk north":

You have to check equality for all of them, not just the first one (the other two "or" are always True, so this condition always is True).

Instead of checking that, it is much simpler to check that "north" is in the user input, check:

if "north" in insideCell:

This is the more pythonic way, plus it is the more robust to spureous user input, it will work even if the user enters "walk to the north", or "travel north"

Upvotes: 0

Ruben Bermudez
Ruben Bermudez

Reputation: 2323

The boolean value of a non-empty string is always true, so if insideCell == "north" or "go north" or "walk north": will be always true because "go north" is true. You can solve it as:

if insideCell in ["north", "go north", "walk north"]:

Make this change for all other ifs or elifs.

Upvotes: 2

Amaury Medeiros
Amaury Medeiros

Reputation: 2233

You can't do:

if insideCell == "north" or "go north" or "walk north":

You should do, instead:

if insideCell == "north" or insideCell == "go north" or insideCell ==  "walk north":

Upvotes: 0

Related Questions