0
Problem with a Python code (basic level)
x=input('Your age:') if 'x'<'18': print('Too young') if 'x'>'60': print('Too old') elif 'x'=='18': print('Perfect!') else: print('Ok,no problem') So, when I run this code and when asks for my age and I give 3, it says 'Too young' which is Ok (3<18) but when I give 18 or something over that , it always shows 'Too old', but I programmed it so that when I will give 18 it will say 'Perfect!' and when I will give something between 18 and 60 it will show 'Ok,no problem'. So what's wrong??
2 Answers
0
you need to make it like this:
x=input('Your age: ')
if x==18:
print ('perfect')
elif x<18:
print ('Too young')
elif x>18:
print ('Too old')
else:
print('ok, no problem')
1. put "if x==18:" first because it will print first True statemant. If you put x<60 before x==18 and input 18 it will print text with x<60 statemant and ignore everything after (x==18)
2. you can have only one if and one else, if you want it to be connected
3. it is better to write:
elif x>18:
print('Too old')
your "elif x<60:" is ok too, if you post it last because if you put "elif x<60:" first and write any number that is less than 60 you will get "Too old" output
edited:
if you want it to print "ok, no problem" for People with age of 60+ replace:
elif x>18:
print ('Too old')
with
elif x>18 and x<60:
print ('Too old')
0
I've tried every way u old, bt the problem does'nt solve.



