I am stuck. Please help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 15

I am stuck. Please help

birthDay = input (“Enter Birth Year”) If birthDay > 1997: Print (“you cannot drink alcohol”) Elif birthDay < 1997: Print (“you can drink alcohol”) I am getting error that < > not supported between instances of str and int

30th Aug 2018, 3:42 AM
Mark Coching
Mark Coching - avatar
36 Answers
+ 16
What if I was born in 1997? (that's my case lol)
30th Aug 2018, 1:50 PM
Eduardo Petry
Eduardo Petry - avatar
+ 9
benjamin A more clear way of doing that: birthYear = input("Enter birth year: ") while not birthYear.isdigit(): birthYear = input("Please, enter a valid year: ") birthYear = int(birthYear)
31st Aug 2018, 8:57 PM
Eduardo Petry
Eduardo Petry - avatar
+ 6
thank you. i ended up typing int. do i put int if i am asking for numbers?
30th Aug 2018, 12:46 PM
Mark Coching
Mark Coching - avatar
+ 6
yes !! int for numbers, float for decimals ^__^
31st Aug 2018, 4:04 PM
Ayesha Qudas
Ayesha Qudas - avatar
+ 4
the int func converts a string to a integer. And the input func gives you a string so you have to convert the string to int with int() func. If the user inputs a text thats not an int an error occurs. Because the int() func can not convert something like ABC into a number
30th Aug 2018, 6:40 PM
Tim
Tim - avatar
+ 3
thank you all. i was able to run it with no issue but inhabe to change my input. i had to ask the user to choose 1 to 100
30th Aug 2018, 10:04 PM
Mark Coching
Mark Coching - avatar
+ 3
Mark Coching 「HAPPY TO HELP」 Ayesha Qudas Tortello Mr-BlackHat Tim Eduardo Petry Guys look closely there r two errors 1) input to be in int so int(input("") and 2nd Print "p" should be lower case ..it will report error ...hope u got it 😎
31st Aug 2018, 5:30 PM
Sri Raman Prasad
+ 2
You should use >= instead of > in the first line of IF block. Like this: If birthDay >= 1997: Also, if you don't have more to compare, use ELSE instead of ELIF.
30th Aug 2018, 10:25 PM
Pedro Tortello
Pedro Tortello - avatar
+ 2
Abhay I think he is referring to the uppercase 'E' on Elif. But this would also apply to the If... and Print...
1st Sep 2018, 5:19 PM
benjamin
+ 2
Abhay Singh what do you mean by exit() in function?? import sys def exxit(): sys.exit() val = int(input(“Press 9 to exit: “)) if val == 9: exxit() You can also use exit() directly
1st Sep 2018, 5:37 PM
Mr-BlackHat
Mr-BlackHat - avatar
+ 2
You cannot compare the numbers and strings with each other. please convert you input into integer so that you can compare. i.e. int(input("enter birth year")) and then try
29th Nov 2020, 3:17 AM
Bikram Ray
Bikram Ray - avatar
+ 1
I would get the input as a string and then use try...except to convert it to an int. This way, if the input cannot be converted to int, you can handle the error instead of your program stopping. Something like: birthDay = input ("Enter birth year: ") while True: try: birthDay = int (birthDay) break except ValueError: birthDay = input ("Please enter a valid year: ") There is other validation you could include such as range check, but this is a good start.
31st Aug 2018, 8:21 PM
benjamin
+ 1
Eduardo, well written and you're right for this example. I was considering a scenario where you might want to catch other errors too by adding more except clauses. Edit. I'm interested if there are better ways to do this than I suggested.
31st Aug 2018, 9:06 PM
benjamin
+ 1
# use this code :- birthDay = int(input("Enter Birth Year")) if 1997 >= birthDay : print("you cannot drink alcohol") else : print("you can drink alcohol") # or birthDay = input("Enter Birth Year") if str(1997) >= birthDay : print("you cannot drink alcohol") else : print("you can drink alcohol") # both will give same output
1st Sep 2018, 3:01 AM
Abhay Singh
Abhay Singh - avatar
+ 1
and there are more solution for this
1st Sep 2018, 3:02 AM
Abhay Singh
Abhay Singh - avatar
+ 1
Inout always produces a type str that's where the error is coming from you need cast to an int for it to work
1st Sep 2018, 6:05 AM
Daniele Catanesi
Daniele Catanesi - avatar
+ 1
I'll Benjamin thankyou for clearing my dought it is giving an error and birthDay is not done in int() it must be like this birthDay = int(input("Enter Birth Year ")
1st Sep 2018, 5:23 PM
Abhay Singh
Abhay Singh - avatar
+ 1
Abhay no problem. The issue you will have is if the input cannot be converted to an integer. This wouuld happen if the user enters alphabetic characters instead of numeric ones.
1st Sep 2018, 5:29 PM
benjamin
+ 1
Input is always taken as stings .So you need to type cast it with int
8th Aug 2020, 5:21 AM
Parth V. Saraf
Parth V. Saraf - avatar