0

I tried making this program.

print("hi! how old are you?")
name = input("enter your age: ")

eighteen = 18
if name < eighteen:
print("damn only " + name + "? you are a little baby")

if name > eighteen:
print("damn, " + name + "? you old")

if name == eighteen:
print("stop lying")

Put I'm getting this error whenever I input a number.

Traceback (most recent call last):
File "C:\Users\moham\Downloads\cli.py", line 5, in <module>
if name < eighteen:
TypeError: '<' not supported between instances of 'str' and 'int'
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

2 Answers2

1

The error is that name is a string variable and eighteen is an int variable. You cannot compare these two. the best way of doing this is:

print("Hi! How old are you?")
age = int(input("Enter age: "))
# this will take the number and make in an integer

if age < 18: #you can use a regular number here
    print("You're too young")
else: # (if you're over 18)
    print("Bro you lyin")

This script works for me and I hope it works for you as well.

Edit: If you input something other than a whole number then it will give you an error

KingTasaz
  • 161
  • 7
0

Like @KingTasaz mentioned, name = input("enter your age: ") reads the input as a string so when you later use name in a comparison (name < eighteen), it throws the error you are seeing.

Their solution will throw a ValueError if the user does not input a number so you can handle that with:

while True:
    try:
        name = int(input("enter your age: "))
        break
    except:
        print('please enter a number')
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245