+ 6

Why the age part isn't giving any output?

name = 'Alice' age = 3000 if name == 'Alice': print ('Hi, Alice!') elif age < 12: print ('You are not Alice, kiddo') elif age > 2000: print ('Unlike you, Alice is not an undead, immortal vampire') elif age > 100: print ('You are not Alice, grannie')

20th Mar 2021, 10:19 AM
Anu
Anu - avatar
14 Answers
+ 9
Why the age part isn't giving any output? name = 'Alice' age = '3000' if name == 'Alice': print ('Hi, Alice!') else: if age < 12: print ('You are not Alice, kiddo') elif age > 2000: print ('Unlike you, Alice is not an undead, immortal vampire') elif age > 100: print ('You are not Alice, grannie')
20th Mar 2021, 10:34 AM
Oma Falk
Oma Falk - avatar
+ 7
Given Code: name = 'Alice' age = '3000' if name == 'Alice': print ('Hi, Alice!') if age < 12: print ('You are not Alice, kiddo') elif age > 2000: print ('Unlike you, Alice is not an undead, immortal vampire') elif age > 100: print ('You are not Alice, grannie') Rectified Code: name = 'Alice' age = 3000 if name == 'Alice': print ('Hi, Alice!') if age < 12: print ('You are not Alice, kiddo') elif age > 2000: print ('Unlike you, Alice is not an undead, immortal vampire') elif age > 100: print ('You are not Alice, grannie') Reason: In line two, age, you declared the value of age with '' which says the computer that it's a string and not a integer value. As now it's a Integer value the if and elif statements can't compare it.
20th Mar 2021, 12:09 PM
ATRS
ATRS - avatar
+ 6
Take care of elif. Only one branch is executed....the Alice branche
20th Mar 2021, 10:23 AM
Oma Falk
Oma Falk - avatar
+ 3
Thankyou so much Frogged! I also removed *else* from 5th line of the corrected code by you. It works!
20th Mar 2021, 10:50 AM
Anu
Anu - avatar
+ 3
Thankyou so much for this explanation Jayakrishna🇮🇳! I didn't know that I was making this silly mistake that you have mentioned in your 2nd point. Thanks!
20th Mar 2021, 10:54 AM
Anu
Anu - avatar
+ 3
Anu Copy Paste the Rectified Code, it will work.
20th Mar 2021, 1:54 PM
ATRS
ATRS - avatar
+ 3
age='3000' is error it should be age=3000 remove single quotes from it.
22nd Mar 2021, 9:17 AM
JavaidIqbal
JavaidIqbal - avatar
+ 2
Frogged and Jayakrishna🇮🇳, can you please check my recent post regarding this? I have asked a question there regarding the same.
20th Mar 2021, 11:20 AM
Anu
Anu - avatar
+ 2
Thanks much JavaidIqbal!
22nd Mar 2021, 1:22 PM
Anu
Anu - avatar
+ 1
Frogged So how can I make it execute?
20th Mar 2021, 10:24 AM
Anu
Anu - avatar
+ 1
if name=='Alice' : if this is true then it won't go to execute else part. On false, it goes to check else part. In your case name=='Alice' is true so it won't check age<12 : So take else separate individual if part like if age<12 : so it is now not depend on previous if. And you are assign age = '3000' , now '3000' is a string type not number. Everything inside quotes is string type. So just do age=3000
20th Mar 2021, 10:35 AM
Jayakrishna 🇮🇳
+ 1
Anu no problem. You're welcome..
20th Mar 2021, 10:58 AM
Jayakrishna 🇮🇳
+ 1
Thankyou for the explanation ATRS!
20th Mar 2021, 12:18 PM
Anu
Anu - avatar
+ 1
Hi all, can someone help me
20th Mar 2021, 6:55 PM
MODE_ _313
MODE_ _313 - avatar