Reputation: 29
I'm learning Python from an online tutorial. My problem is that when I run the script, no matter what I input the response I get is the if go == "kitchen"...
def go_to():
go = raw_input("Go to? ")
if go == Kitchen or breakfast:
print "You rumble down stairs and into the kitchen. Your mom has left some microwaved waffles on the table for you. Your big boy step sits by the counter."
elif go == "back to bed" or "back to sleep" or bed or sleep:
print "You hit snooze and roll over."
elif go == "bathroom" or "toilet" or "potty" or "pee" or "poop" or "take a sh*t" or "take a dump" or "drop a load":
print "You make a stop at the head first."
go_to()
else:
print "That is not a command I understand."
go_to()
go_to()
Upvotes: 3
Views: 2341
Reputation: 52
What your code says is that if go in kitchen or breakfast So for python it means if go in kitchen or if breakfast
so, it's either Something or True.
it resolves to the True and thus the first if statement always gets executed.
You can fix it by
if go in ['kitchen', 'breakfast']
Upvotes: 0
Reputation: 142156
To check if a condition is one of a list of things, then you should use in
, eg:
if go in ('Bathroom', 'take a dump', 'have a beep'):
# do something...
elif go in ('Kitchen', 'Living room'):
# etc...
else:
# no idea where to go?
The use of or
doesn't do what you expect and has been explained in other posts.
Upvotes: 0
Reputation: 4780
The syntax
if go == "Kitchen" or "breakfast":
is wrong because of the order of evaluation. It seems you intend to check if go is either "kitchen" or "breakfast". However, you check if go is "kitchen" or the string "breakfast" is true. The latter is always the case, because a non-empty string evaluates to something that is not False.
The intuitive way of describing your intent would be:
if (go == "kitchen") or (go == "breakfast"):
Probably more pythonic you could also write:
if go in ["kitchen", "breakfast"]:
Upvotes: 0
Reputation: 49846
As Ignacio says, you need a new tutorial.
The expression go == Kitchen or breakfast
will be true if either of the subexpressions go == Kitchen
or breakfast
evaluate to True
. This will happen if go
evaluates to the same object as Kitchen
, or their type defines an __eq__
method which defines equality for them, or it will be the case if breakfast
is an object that is not None
.
The way to check if a variable contains a value in a list is:
if go in (Kitchen, breakfast):
# do something
Note also that your code doesn't show where the variables Kitchen
and breakfast
are defined, and your indentation is incorrect.
Upvotes: 1
Reputation: 3911
As was mentioned in the comments, your use of or
is incorrect here. This:
go == "kitchen" or "breakfast"
Is equivalent to this:
(go == "kitchen") or "breakfast"
The or
operator casts both of its operands to booleans, which gives you:
(something) or true
That always reduces to true
, so you always enter the if statement
Upvotes: 5
Reputation: 7921
i beleive the problem is with your first if statement
in most programming languages you can't just say "or" like you do. what you need to do is repeat the "go ==" part for all conditions.
if go == Kitchen or go == breakfast:
what it is doing right now is evaluating (go == Kitchen) and finding it false. then it is evaluating "breakfast" and that returns true. since it is an "or" statement then the entire if is true.
Upvotes: 0