+ 1

Help me, please. TypeError

It is from Python2 if guess < secret: There it runs. But an error appears in Python3 TypeError: '<' not supported between instances of 'str' and 'int' What is right in the Python3?

31st Jan 2019, 8:55 AM
Слава Сибиряк
4 Réponses
+ 3
Maybe python 3 did some changes on relational operators. As the error says you are going to compare a string and a integer. The best way to overcome this problem is converting the values to a same type using int(). If you provided the full code we could help you more!
31st Jan 2019, 9:07 AM
Seniru
Seniru - avatar
+ 3
Line 8: guess = input( ... guess is now a string Line 9: if guess < secret: Comparison between string and integer. Here is your error. Try changing line 8: guess = int(input( ...
31st Jan 2019, 10:06 AM
Pedro Tortello
Pedro Tortello - avatar
+ 2
Yes, everything works! Thanks a lot Tortello and Seniru Pasan !
31st Jan 2019, 10:34 AM
Слава Сибиряк
+ 1
Here is the full code. This is an example from the book about Python2 import random secret = random.randint(1, 99) guess = 0 tries = 0 print "Эй на палубе! Я Ужасный пират Робертс, и у меня есть секрет!" print "Это число от 1 до 99. Я дам тебе 6 попыток." while guess != secret and tries < 6: guess = input("Твой вариант?") if guess < secret: print "Это слишком мало, презренный пес!" elif guess > secret: print "Это слишком много, сухопутная крыса!" tries = tries + 1 if guess == secret: print "Хватит! Ты угадал мой секрет!" else: print "Попытки кончились!" print "Это число ", secret And it comes out when I start the IDLE. Python 3.7.2 Эй на палубе! Я Ужасный пират Робертс, и у меня есть секрет! Это число от 1 до 99. Я дам тебе 6 попыток. Твой вариант 90 Traceback (most recent call last): File "F:\Users\Слава\Desktop\pYthOn\NumGuess.py", line 9, in <module> if guess < secret: TypeError: '<' not supported between instances of 'str' and 'int'
31st Jan 2019, 9:46 AM
Слава Сибиряк