0

I'm trying to learn more about if statements. I tried to create a program that asks the user to enter amounts of beer and outputs different answers when the amount increases. I can't figure out what I'm doing wrong.

What I expect to happen is when I type in the number eg. 5 the result should be "Beer is awesome." because it's lower than 50. And if I type in 60 it should output "That's a lot of beer." because it's higher than 50 and lower than 100.

I've read the Python documentations and still don't figure it out. This is my code:

print "How many cans of beer do you want?"

beer = raw_input("Enter your amount: ")

if beer >= 1:
    print "Beer is awesome."
elif beer >= 50:
    print "That's a lot of beer."
elif beer >= 100:
    print "You're drinking way too much beer, man."
else:
    "Did you make a choice larger than 100? If yes, then you're crazy!"

4 Answers 4

5

The raw_input returns a string, you should convert to an int to do math/operations with it.

beer = int(raw_input("Enter your amount: "))

Also, always mention what goes wrong/against your expectations in your post, in this case it's easy to see but with more complex code it gets hard quickly what the actual problem is.

Edit: There's another problem with your program, but I think you'll be able to figure out that one on your own when you run it without errors.

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

3 Comments

Thank you Azeirah! It worked out well. Yes i will mention this in the future.
@Cybe just in case, the other problem is that any value >= 50 or >= 100 is also >= 1, since those conditions are on elif clauses those blocks will never be executed.
@Azeirah yes i figured that out and as i commented in Daans answer. I used <= instead of >=. It works perfect.
0

You should use upper bounds instead of lower bounds, otherwise your first if is always true and you get "Beer is awesome."

if beer < 50:
    print "Beer is awesome."
elif beer < 100:
    print "That's a lot of beer."
elif beer < 500:
    print "You're drinking way too much beer, man."
else:
    "Did you make a choice larger than 500? If yes, then you're crazy!"

Comments

0

Let's see what your if statements do.

if beer >= 1:

Which means that when the value of beer is larger than or equal to one, the following code is executed.

elif beer >= 50

elif is a keyword that you can find in other languages as "else if". It means something like: if the previous statement was false (else) but the following is true (if), execute the following block. Which means that you only get the output "That's a lot of beer." when beer >= 1 is false, and beer >= 50 is true - which is never, because beer can't be smaller than 1 and larger than 50 at the same time. The same holds for the other elif statements.

You may get the last output, "Did you make a choice larger than 100? If yes, then you're crazy!", when all other statements are false - in other words, when beer is equal to or less than zero.

I hope you understand how you can get the correct statements now, to get what you want.

1 Comment

Thank you Daan. I've figured it out and put <= instead of >= and added a new elif and removed the text in the else: if beer <= 10: print "Beer is awesome." elif beer <= 50: print "That's a lot of beer." elif beer <= 100: print "You're drinking way too much beer, man." elif beer >= 100: print "Did you make a choice larger than 100? If yes, then you're crazy!" else: print ""
0

The problem is that raw_input returns a string -= to make numeric comparisons to the value returned yiou have to convert it to int.

try changing your input line to beer = int(raw_input("Enter your amount: ")) instead

1 Comment

That code line is funny. You didn't change anything at all =)

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.