0
Why am I getting an error on my code below
#generate a random number between 1 and 20 and task the user to guess the number, then tell them whether the they guessed too low, too high or exactly right. import random num = random.randint(1, 20) while True: print("Guess a number between 1 and 20") guess = input() i = int(guess) if i ==num: print("exactly rightly") break elif i < num: print("too low, try again!") elif i > num: print("too high, try again!")
6 Answers
+ 6
praneetha ,
your comment is not correct, we don't need to use an *alias* name for an imported module. if there is an issue by running the code, it results from something else.
+ 4
there is no issue with the code shown by Gift Claire, it does run properly in a regular ide, but not in sololearn playground!
playground requires to enter all input values prior to the code execution. in the current case it makes no sence to use playground, since the code is done for interactiv use.
Tiya , to remove the loop from the code makes it easy to use in playground, but the aim of the code (interaction between code and user) can not be done withouf a loop.
+ 2
Lothar ok, thanks for letting me know đ
0
Ohh okay. Thank you
0
It's because of the loop declaration probably. Try this:
import random
num = random.randint(1, 20)
print("Guess a number between 1 and 20")
guess = input()
i = int(guess)
if i ==num:
print("exactly right!")
elif i < num:
print("too low, try again!")
elif i > num:
print("too high, try again!")
0
Lothar Ah, I said that because I didn't understand the code was supposed to be running till the correct answer. Thanks