Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
elif and if
#1
this code works fine except the last elif

elif age >= '100' and name != 'Alice':
print('you are not alice grannie')
If I enter any value between 100 and 119 it returns 'you are not Alice kiddo' however if I enter 120 or above it returns the proper value. what am I doing wrong?



print ('name please') #ask for name
name=input()
print ('age please') #ask for age
age=input()
if name == 'Alice' and age == '12':

print ('Hi Alice')


elif age < '12' and name != 'Alice':
print ('you are not alice, kiddo')
elif age > '2000' and name != 'Alice':
print ('unlike you alice is not an undead, vampire')
elif age >= '100' and name != 'Alice':
print('you are not alice grannie')
Reply
#2
Please put your code in Python code tags. You can find help here.
Reply
#3
this code works fine except the last elif


elif age >= '100' and name != 'Alice':
print('you are not alice grannie')
If I enter any value between 100 and 119 it returns 'you are not Alice kiddo' however if I enter 120 or above it returns the proper value. what am I doing wrong?


print ('name please') #ask for name
name=input()
print ('age please') #ask for age
age=input()
if name == 'Alice' and age == '12':

print ('Hi Alice')


elif age < '12' and name != 'Alice':
print ('you are not alice, kiddo')
elif age > '2000' and name != 'Alice':
print ('unlike you alice is not an undead, vampire')
elif age >= '100' and name != 'Alice':
print('you are not alice grannie')
Reply
#4
You are comparing strings, not integers.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
You are doing string comparisons and you need to do numeric comparisons.

Try something like:
age = int(input('age please'))
then remove the quotes from around your number comparison values.

Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,234 Jul-28-2019, 05:52 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020