Reputation: 107
I'm trying to make a program that will respond to someones age but I can't figure out how to use the comparison symbols. Here is the code I'm trying to run,
age = input('How old are you? \n >>')
if (age < 20):
print('Hey you are pretty young.')
if (age > 20):
print('wow you are pretty old')
But when I try to run this I get this Error,
Traceback (most recent call last):
File "C:/Users/Daniel/Desktop/Computer science/week 6/age.py", line 2, in <module>
if (age < 20):
TypeError: '<' not supported between instances of 'str' and 'int'
Upvotes: 2
Views: 192
Reputation: 43169
What you get as input is a string, you need to cast it to an int before you can compare it:
age = int(input('How old are you? \n >>'))
Better yet, add some error handling, e.g.:
try:
age = int(input('How old are you? \n >>'))
except ValueError as ex:
print("Not a valid age.")
Upvotes: 3