Differnces between 'and ' & ' non-and'?


I’m learning if, elif, else in Python and making drinking age caculator in a class. That’s a code I just made.

age = int(input("How old are you?"))
if age < 18 :
 print ("You can't drink.")
else :
  print ("Go ahead!")

I wanted to put a recommendation to drink wine for age 18~35, so created a code.

if 18<=age<=35 :
  print ("We recommend you to drink a glass of grape wine!")

However, in the lecture, they put a differential code.

if age >=18 and age<=35 :
  print ("We recommend you to drink a glass of grape wine!")

In sum, I wonder the differences between the codes that I created and they fomulated.

Thanks for reading.

Does that code work? I don’t think it would, but maybe I’m wrong.

1 Like

Hi. When I put number 18, it says “We recommend you to drink a glass of grape wine!” as I coded. It works though. Do you think it is safe to think that and is not nessary when I put some limitations in number(int)?

Hi @aleich067056 !
I tested both codes, and I found that the one that you formulated correctly outputs the text if the user’s age was 22, for example.

Hi @aleich067056 ! Could you explain what you meant by:


I’m curious, as I’d like to do ifs like that if possible. What are the outputs when the inputs are 17, 22, and 40?

Thanks for your attention.

It means just to make more limit in numbers(1,2,3,4… ,<-- are they called ‘int’, aren’t they? ) on a variable.
(ex. age is variable → 1<age<6 43<age<66)—> put limitations on number(int)

Python supports chained comparison, the results are ‘and-ed’ together.

1 <= x < 3 is the same as 1 <= x and x < 3


As I coded, it is subject to “else” which is located in the first code, so says “Go ahead!”, when put 22, 40. But if you put 17, it would say “You can’t drink.”

You learn something every day. Thanks for the information!

1 Like

I have to learn everyday. Thank you for your reply. Have a nice day.

Hi @aleich067056 !
If your problem is solved, you can mark the post that helped you most as the Solution.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.