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
8/30/2018 3:42:04 AMMark Coching
34 AnswersNew Answer
What if I was born in 1997? (that's my case lol)
yes, int for numbers, float for decimals :)
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)
thank you. i ended up typing int. do i put int if i am asking for numbers?
yes !! int for numbers, float for decimals ^__^
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
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
Mark Coching 「ＨＡＰＰＹ ＴＯ ＨＥＬＰ」 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 😎
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.
Abhay I think he is referring to the uppercase 'E' on Elif. But this would also apply to the If... and Print...
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
Abhay, have a read of https://stackoverflow.com/questions/6501121/difference-between-exit-and-sys-exit-in-python
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.
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.
# 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
and there are more solution for this
Inout always produces a type str that's where the error is coming from you need cast to an int for it to work
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 ")
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.