import random
n = 1
tala = random.randrange(0,11)
print('talan er ' + str(tala))
print('The goal is to guess the number.')
num = input('Pick a number: ')
while num != tala:
if num < tala:
print('Too Low')
num = input('Try again: ')
elif num > tala:
print('Too High')
num = input('Try again: ')
else:
print('wow congratulations, you guessed the right number in ' + str(n) + ' tries.')
n += 1
Above is my code, and below is the error that I am encountering.
I can't seem to find my error which is: It's just a simple code, but the error is something I can't find.
Traceback (most recent call last):
File "C:/Users/ebben/PycharmProjects/HelloWorld/GuessNumber.py", line 10, in <module>
if num < tala:
TypeError: '<' not supported between instances of 'str' and 'int'
-
What do/don't you understand from that error message?AMC– AMC2020年03月23日 21:55:38 +00:00Commented Mar 23, 2020 at 21:55
-
Does this answer your question? How can I read inputs as numbers?AMC– AMC2020年03月23日 21:56:05 +00:00Commented Mar 23, 2020 at 21:56
2 Answers 2
python input is always string and you need to convert it to integer by adding int() method
here is your code:
you need to change num:
import random
n = 1
tala = random.randrange(0,11)
print('talan er ' + str(tala))
print('The goal is to guess the number.')
num = int(input('Pick a number: '))
while num != tala:
if num < tala:
print('Too Low')
num = int(input('Try again: '))
elif num > tala:
print('Too High')
num = int(input('Try again: '))
else:
print('wow congratulations, you guessed the right number in ' + str(n) + ' tries.')
n += 1
answered Mar 23, 2020 at 19:44
A._.P._.G
1021 gold badge2 silver badges10 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
The error show that less than < operator and > operator must be compared with number data type which either int or float. which mean variable num is a string object.
So replace the following line to solved your error:-
Replace
if num < tala:
into
if int(num) < tala:
&
Replace
if num > tala:
into
if int(num) > tala:
answered Mar 23, 2020 at 18:20
Ronnie Tws
4843 silver badges6 bronze badges
Comments
lang-py