+ 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')
14 Réponses
+ 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')
+ 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.
+ 6
Take care of elif.
Only one branch is executed....the Alice branche
+ 3
Thankyou so much Frogged! I also removed *else* from 5th line of the corrected code by you. It works!
+ 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!
+ 3
Anu Copy Paste the Rectified Code, it will work.
+ 3
age='3000' is error it should be age=3000 remove single quotes from it.
+ 2
Frogged and Jayakrishna🇮🇳, can you please check my recent post regarding this? I have asked a question there regarding the same.
+ 2
Thanks much JavaidIqbal!
+ 1
Frogged So how can I make it execute?
+ 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
+ 1
Anu no problem.
You're welcome..
+ 1
Thankyou for the explanation ATRS!
+ 1
Hi all, can someone help me