3

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()
9
  • 5
    You need a new tutorial, since that usage of or is incorrect. Commented Aug 2, 2012 at 15:34
  • 2
    As it stands, this program will abort with a Syntax error. Indentation is significant in Python. Please fix that. Also, you have things like bed and sleep which are unquoted. That will error out too when your function is called. Commented Aug 2, 2012 at 15:36
  • 1
    You don't like " and ' chars, do you? Commented Aug 2, 2012 at 15:41
  • 1
    why did someone downvote? it's a valid question, no matter how simple. unless because of duplicates. Commented Aug 2, 2012 at 15:42
  • 1
    @user1571810, This is a good python tutorial: please use it Commented Aug 2, 2012 at 16:14

6 Answers 6

5

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

yeah, they werent meant to be variables, but strings. i was just trying different things and took the quotes off and forgot to put them back on. thanks for the help.
@user1571810 It doesn't matter whether they are variables or strings. The same evaluation rules apply (except of course that there is an extra step of evaluation).
0

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.

Comments

0

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"]:

Comments

0

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.

Comments

0

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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.