Why am i getting NameError? in While Loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why am i getting NameError? in While Loop

Why am i getting NameError? I am looking to run a while loop till user enters 4 or stop... my code: #Guess game, Guss the number ot enter stop to stop the game lucky_num = 4 guess = eval(input("Enter a number: ")) while guess != lucky_num or guess == "stop": guess = eval(input ("enter again: ")) print ("Done") https://code.sololearn.com/cArrC7NFgZmP/#py

2nd Jul 2020, 1:32 AM
Pavneet Ghai
Pavneet Ghai - avatar
5 Answers
+ 3
1. Do not use eval 2. While condition is wrong, use AND not OR, use guess!="stop", not guess=="stop" 3. Invert the 2 terms of the condition to avoid int() error when you enter "stop" https://code.sololearn.com/ct6dFUh4qbha/?ref=app
2nd Jul 2020, 4:47 AM
Bilbo Baggins
Bilbo Baggins - avatar
+ 2
Because you're trying to evaluate "stop", which is an unknown command unless you implement it.
2nd Jul 2020, 2:45 AM
Sandra Meyer
Sandra Meyer - avatar
+ 2
even if i dont use eval() it gives error how do i code without using if condition in while loop?
2nd Jul 2020, 3:13 AM
Pavneet Ghai
Pavneet Ghai - avatar
+ 2
You better rearrange your code a bit. eval is completely useless. You get another exception because you're mixing the types and access input though there is none more.
2nd Jul 2020, 3:35 AM
Sandra Meyer
Sandra Meyer - avatar
+ 2
so u mean i. while condition there should be only 1 data type?
2nd Jul 2020, 3:37 AM
Pavneet Ghai
Pavneet Ghai - avatar