Python Forum

Full Version: '<=' not supported between instances of 'str' and 'int'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've just started learning Python and I try to run a program that is written, more or less, in the book i'm using right now (Automate the boring stuff with Python). This is the code

name=''
age=''
print ('what is your name?')
name==input()
if name=='Alice':
    print ('hi alice')
else:
    print ('hello stranger, what is your age?')
age==input()
if age <= 18:
    print ('come right in')
else:
    print ('you cannot enter')
The code in the book is this:

if name == 'Alice':
    print('Hi, Alice.')
elif age < 12:
    print('You are not Alice, kiddo.')
elif age > 2000:
    print('Unlike you, Alice is not an undead, immortal vampire.')
elif age > 100:
    print('You are not Alice, grannie.')
I know he's using the elif function, but I tried to run this as well and I get the same error. How should I write so that i don't get that error? I tried with int function in front of age input and it doesn't work.
(Aug-11-2018, 05:40 PM)Loom Wrote: [ -> ]I tried with int function in front of age input and it doesn't work.
that is exactly what you should do - convert age input (which comes as str) to int. It didn't work because you use comparison ==, not assignment =.

age = int(input())
same for the name
name = input()
name = input('what is your name? ')
if name == 'Alice':
    print ('hi alice')
else:
    print ('hi stranger')
age = int(input('what is your age? '))
if age <= 18:
    print ('come right in')
else:
    print ('you cannot enter')