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,6987 gold badges15 silver badges35 bronze badges
asked Mar 23, 2020 at 18:03
2
  • What do/don't you understand from that error message? Commented Mar 23, 2020 at 21:55
  • Does this answer your question? How can I read inputs as numbers? Commented Mar 23, 2020 at 21:56

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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:
answered Mar 23, 2020 at 18:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.