-2
import random 
user_name=("enter your name:")
print("Note: The number lies between 1 to 100")
random_nuber = random.randrange(1, 100, 1)
user_number = input("enter your guess")
dif= user_number - random_nuber

while dif != 0:
if dif > 0:
    print("high")
if dif < 0:
    print("low")
user_number = input("enter your guess now :")

I am a beginner and everytime I use while or for loop, I get the same kind of error.

the error I get each time I run this code

Tiramisu
  • 109
  • 1
  • A traceback is *text*, please copy and paste it as such into your question. Then we can index (make it searchable), visually impaired people can help you (as their screenreader can read the text) and we can copy and paste filenames. – Martijn Pieters Jun 25 '17 at 07:59
  • The error already describes it, from input you get the string user_number and random_number is a int. In order to subtract one from another you need to convert that string into an int – danielspaniol Jun 25 '17 at 08:00

1 Answers1

-1

input("enter your guess") is returning a string, so user_number - random_nuber won't work as they are different types - a number and a string.

EDIT: For anyone who's used to Python2, this normally tries to evaluate the string - so if you input a number, it automatically converts it to a number. In Python3 (as per the question), it is always a string and thus needs to be converted.

You need to convert the number into a string, either by changing it to an integer when you make it: user_number = int(input("enter your guess")), or by changing it when you need it in the operation: dif= int(user_number) - random_nuber

(Personally, I'd recommend the first option)

Nabushika
  • 364
  • 1
  • 17