0

My code is as follows:

prompt = '\nPlease tell us your age'
prompt += "\n(Enter 'quit' when you are finished)"

while True:
    age = input(prompt)

    if age == 'quit':
        break
    elif int(age) < 3:

        print ('Your admission is free')
    elif int(age) > 3 < 12:
        print ('Your admission charge is $10')
    else:

        print ('Your admission charge is $15')

    break

Probably a simple answer, but when age entered greater than 12, returns 'Your admission charge is $10' when 'Your admission charge is $15' is expected - Why ?

1
  • elif 3 < int(age) < 12:? Commented Jul 23, 2019 at 11:11

4 Answers 4

2

When you have several condition after your elif statement, the syntax is a bit different :

while True:
   age = input(prompt)
   if age == 'quit':
       break
   elif int(age) < 3:
       print ('Your admission is free')

   elif (int(age) > 3) and (int(age) < 12):
   # Annother possibility :
   #elif 3 < int(age) < 6 : 
       print ('Your admission charge is $10')
   else:
       print ('Your admission charge is $15')

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

Comments

0

Probably you have to use AND operator.

elif int(age) > 3 and int(age) < 12:
...

I also recommend casting the input right after you read it - you don't have to write int(age) every time :)

age = int(input(prompt))

Comments

0

Its because its goint to else condition for example i put 13, first condition is incorrect going to second, 13 < 3 false, going to third, 13 > 3 here true < 12 here false, the condition is false, now going to else and make the stuff you want on this else in your case the print.

Good example of this situation is use of assert:

assert 13 > 3 and 13 < 12

Comments

0

Try 3 < age < 12

Your int(age) > 3 < 12 is interpreted as int(age) > 3 && 3 < 12 which is true for all ages over 3

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.