Reputation: 84
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'
Upvotes: 0
Views: 37
Reputation: 102
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
Upvotes: 2
Reputation: 484
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:
Upvotes: 0