Python Forum
TypeError: '<=' not supported between instances of 'str' and 'int'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: '<=' not supported between instances of 'str' and 'int'
#1
Hi, I am new to the programing scene and I've come across a problem. I am working on a program that will take in five readings from the user. If the reading is between 80 and 100 the program should display "S", if it is between 30 and 80 it should display "M", and if it is between 0 and 30 it should display "P". I made the program using arrays just as my teacher instructed me to (this is an assignment for Computing Science. The program works fine if the number entered is 30 or below, however any higher and I get something on the lines of this
Traceback (most recent call last):
  File "C:\Users\Filip\Desktop\program design.py", line 13, in <module>
    if readingOne <= 30 and readingOne >= 0:
TypeError: '<=' not supported between instances of 'str' and 'int'
here is the code:
readingPattern = ["S", "M", "P"]


#Area 1
readingOne = int(input("Please enter the first reading: "))

if readingOne <= 100 and readingOne >= 80:
   readingOne = readingPattern [0]
   
if readingOne < 80 and readingOne > 30:
    readingOne = readingPattern [1]
    
if readingOne <= 30 and readingOne >= 0:
    readingOne = readingPattern [2]


#Area 2
readingTwo = int(input("Please enter the second reading: "))
if readingTwo <= 100 and readingTwo >= 80:
    readingTwo = readingPattern [0]
if readingTwo < 80 and readingTwo > 30:
    readingTwo = readingPattern [1]
if readingTwo <= 30 and readingTwo >= 0:
    readingTwo = readingPattern [2]



#Area 3
readingThree = int(input("Please enter the third reading: "))
if readingThree <= 100 and readingThree >= 80:
    readingThree = readingPattern [0]
if readingThree < 80 and readingThree > 30:
    readingThree = readingPattern [1]
if readingThree <= 30 and readingThree >= 0:
    readingThree = readingPattern [2]


#Area 4
readingFour = int(input("Please enter the fourth reading: "))
if readingFour <= 100 and readingFour >= 80:
    readingFour = readingPattern [0]
if readingFour < 80 and readingFour > 30:
    readingFour = readingPattern [1]
if readingFour <= 30 and readingFour >= 0:
    readingFour = readingPattern [2]


#Area 5
readingFive = int(input("Please enter the fifth reading: "))
if readingFive <= 100 and readingFive >= 80:
    readingFive = readingPattern [0]
if readingFive < 80 and readingFive > 30:
    readingFive = readingPattern [1]
if readingFive <= 30 and readingFive >= 0:
    readingFive = readingPattern [2]


print (readingOne, readingTwo, readingThree, readingFour, readingFive)

nevermind i just realised that i forgot that it was supposed to be elif.

this is the result of sleep deprivation
Reply
#2
nice that you solve your problem. what about refactoring it to avoid repetition and this huge if/elif/else? Maybe using dict for reading pattern?
Reply


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

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020