0
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'
AMC
  • 2,642
  • 7
  • 13
  • 35
Gandalf
  • 94
  • 1
  • 1
  • 6
  • What do/don't you understand from that error message? – AMC Mar 23 '20 at 21:55
  • Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – AMC Mar 23 '20 at 21:56

2 Answers2

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
A._.P._.G
  • 92
  • 1
  • 10
0

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:
Ronnie Tws
  • 464
  • 3
  • 6