Python Forum
'<=' not supported between instances of 'str' and 'int'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'<=' not supported between instances of 'str' and 'int'
#1
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.
Reply
#2
(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')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort Function: <' not supported between instances of 'float' and 'tuple' quest 2 11,120 Apr-30-2021, 07:37 PM
Last Post: quest
Exclamation TypeError: '>=' not supported between instances of 'int' and 'str' helpme1 11 13,245 Mar-11-2021, 11:13 AM
Last Post: helpme1
  Type error: '>' not supported between instances of 'NoneType' and 'int' spalisetty06 1 12,570 Apr-29-2020, 06:41 AM
Last Post: buran
  TypeError: '<' not supported between instances of 'str' and 'int' Svensation 5 13,960 Jan-20-2020, 08:12 PM
Last Post: buran
  TypeError: Not supported between instances of 'function' and 'int' palladium 9 25,375 Dec-06-2019, 12:40 AM
Last Post: palladium
  TypeError: '>=' not supported between instances of 'str' and 'int' AsadZ 8 17,447 Aug-20-2019, 11:45 AM
Last Post: ThomasL
  '>' not supported between instances of 'str' and 'int' graham23s 2 5,413 May-11-2019, 07:09 PM
Last Post: micseydel
  Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int sr12 8 19,668 Apr-11-2019, 08:19 PM
Last Post: sr12
  '<' not supported between instances of 'str' and 'int' jayaherkar 1 9,083 Apr-09-2019, 03:25 PM
Last Post: perfringo
  TypeError: '<' not supported between instances of 'int' and 'builtin_function_or_meth yann2771 1 7,381 Mar-05-2019, 09:51 PM
Last Post: stranac

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.