Why am I getting an error on my code below | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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!")

16th Jul 2022, 1:52 PM
Gift Claire
Gift Claire - avatar
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.
16th Jul 2022, 6:19 PM
Lothar
Lothar - avatar
+ 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.
18th Jul 2022, 9:53 AM
Lothar
Lothar - avatar
+ 2
Lothar ok, thanks for letting me know 👍
17th Jul 2022, 2:59 AM
praneetha
praneetha - avatar
0
Ohh okay. Thank you
16th Jul 2022, 2:13 PM
Gift Claire
Gift Claire - avatar
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!")
18th Jul 2022, 7:23 AM
Tiya
Tiya - avatar
0
Lothar Ah, I said that because I didn't understand the code was supposed to be running till the correct answer. Thanks
18th Jul 2022, 10:04 AM
Tiya
Tiya - avatar