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
Dear all

I just tried my first steps with Python and found a - on the first sight - simple example in the internet. I tried to do it exactly the same way mentioned on the page. The programme looks as follows:

secret = 10
guess =0
i = 0

while guess != secret:
    guess = input("Raten Sie: ")
    if guess < secret:
        print ("Zu klein")
    if guess > secret:
        print ("Zu gross")
    i = i + 1
print ("Super, Sie haben es in "), i, ("Versuchen geschafft!")
Unfortunately it shows me the message: TypeError: '<' not supported between instances of 'str' and 'int'

What does that mean? What is wrong?

Thx for any help!
Reply
#2
input() will return str. you need to convert it to int in order t do the comparison
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
#3
Hi Buran

Thx for the information, will do better next time.

As I said I am an real beginner. What do you mean with int? where do I have to place that?
Reply
#4
the built-in function int()

guess = int(input("Raten Sie: "))
note that incorrect input, that cannot be converted to int will raise an exception
Error:
ValueError: invalid literal for int() with base 10
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
#5
thx a lot, it works!

Except the last line, as it doesn't count the number of trials. It only showes me the first piece of the print command.
Reply
#6
it should be
print("Super, Sie haben es in ", i, "Versuchen geschafft!")
however it's waaaaaaay better to use string formatting, e.g.

using f-strings (python3.6+):
print(f"Super, Sie haben es in {i} Versuchen geschafft!")
or str.format() method:
print("Super, Sie haben es in {} Versuchen geschafft!".format(i))
see
https://docs.python.org/3/library/string...ing-syntax
https://pyformat.info/
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 8,086 Apr-30-2021, 07:37 PM
Last Post: quest
Exclamation TypeError: '>=' not supported between instances of 'int' and 'str' helpme1 11 8,815 Mar-11-2021, 11:13 AM
Last Post: helpme1
  Type error: '>' not supported between instances of 'NoneType' and 'int' spalisetty06 1 10,486 Apr-29-2020, 06:41 AM
Last Post: buran
  TypeError: Not supported between instances of 'function' and 'int' palladium 9 19,644 Dec-06-2019, 12:40 AM
Last Post: palladium
  TypeError: '>=' not supported between instances of 'str' and 'int' AsadZ 8 10,530 Aug-20-2019, 11:45 AM
Last Post: ThomasL
  '>' not supported between instances of 'str' and 'int' graham23s 2 3,990 May-11-2019, 07:09 PM
Last Post: micseydel
  Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int sr12 8 13,092 Apr-11-2019, 08:19 PM
Last Post: sr12
  '<' not supported between instances of 'str' and 'int' jayaherkar 1 7,947 Apr-09-2019, 03:25 PM
Last Post: perfringo
  TypeError: '<' not supported between instances of 'int' and 'builtin_function_or_meth yann2771 1 5,565 Mar-05-2019, 09:51 PM
Last Post: stranac
  '<=' not supported between instances of 'str' and 'int' Loom 1 7,780 Aug-11-2018, 07:34 PM
Last Post: buran

Forum Jump:

User Panel Messages

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