+ 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
37 Answers
+ 16
What if I was born in 1997? (that's my case lol)
+ 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)
+ 6
thank you. i ended up typing int. do i put int if i am asking for numbers?
+ 6
yes !! int for numbers, float for decimals ^__^
+ 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
+ 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
+ 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 😎
+ 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.
+ 2
Abhay I think he is referring to the uppercase 'E' on Elif. But this would also apply to the If... and Print...
+ 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
+ 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
+ 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.
+ 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.
+ 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
+ 1
and there are more solution for this
+ 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
+ 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 ")
+ 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.
+ 1
Input is always taken as stings .So you need to type cast it with int