3

RESOLVED

When an integer containing "0" or "4" is entered, this if-statement only returns the first in the statement.

For example, in the code below, if I enter "60", it will execute:

print "Nice, you're not greedy - you win!" exit(0)

NOT

dead("You greedy bastard!")

as I expected with how_much >= 50.

Have tried a bunch of changes, but can't seem to get to execute as intended. Anyone know what's going on here?

def gold_room():
    print "This room is full of gold. How much do you take?"
    number_type = False

    while True:

        choice = raw_input("> ")

        how_much = int(choice)

        if "0" in choice or "4" in choice and how_much < 50:
            print "Nice, you're not greedy - you win!"
            exit(0)
        elif "0" in choice or "4" in choice and how_much >= 50:
            dead("You greedy bastard!")
        else:
            print "Man, learn to type a number. Put a 0 or a 4 in your number."

5 Answers 5

2

You have an order-of-operations issue. The and operator binds more tightly than the or operator, so when you write:

  if "0" in choice or "4" in choice and how_much < 50:

You are actually getting:

  if ("0" in choice) or ("4" in choice and how_much < 50):

And hopefully, with those parentheses, it's obvious why entering 60 triggers the "Nice, you're not greedy - you win!" message (because it matches the "0" in choice coindition, and since that condition is true, the entire or statement is true).

Add parentheses to get what you want:

  if ("0" in choice or "4" in choice) and how_much < 50:

See this article for details.

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

1 Comment

Thanks, this explanation was very helpful!
1

You need some brackets in your conditionals to make sure they get evaluated the way you want, e.g:

    if ("0" in choice or "4" in choice) and how_much < 50:

You'll need something similar on the next condition too.

1 Comment

Thanks, that took care of it!
1

You should separate your conditions into logical groups. Besides, you have repeated condition "0" in choice or "4" in choice, use an optimized structure as shown below:

if "0" in choice or "4" in choice:
    if how_much < 50:
        print "Nice, you're not greedy - you win!"
        exit(0)
    elif how_much >= 50:
        dead("You greedy bastard!")
else:
    print "Man, learn to type a number. Put a 0 or a 4 in your number."

1 Comment

Thanks - your grouping lens is very helpful!
0

That's because the and is executed before the or https://docs.python.org/3/reference/expressions.html#operator-precedence Some () in the correct places will fix this.

It'll be easier to test this too if you split the test in a different function

Comments

0

Python will do this: choice = '60' how_much = 60 if "0" in choice (Will return True) OR "4" in choice AND how_much < 50 (This will return false but since you did that or it will continue with it) You should do it like this instead :

if ("0" in choice or "4" in choice) and how_much < 50:
        print "Nice, you're not greedy - you win!"
        exit(0)
elif how_much >= 50:
        dead("You greedy bastard!")

The parantheses will make it so only if it will return True it will then compare to see if the variable "how_much" is less then 50. Before , it was checking if "0" is in choice OR if the 4 in choice and how_much is less then 50. The OR makes it so only one of the statements have to be True to continue on the next line of code (The AND makes it so the "4 in choice and how_much < 50" will only return True or False wich will have to be compared to the "0 in choice")

Sorry if it doesn't make a lot of sense but I'm sleepy Hope you got the idea.

1 Comment

Thanks, that did it!

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.