Why won't this work?

non-track-course
blackjack

#1



Why wont this work?


It says "Traceback (most recent call last):
File "python", line 19, in
NameError: name 'decision' is not defined" whenever I run it.


while decision != "yes" or decision != "no":
    
    decision = input("You can still do it, though!  Do you keep going?")
    
    if(decision == "no"):
        print("Good bye. =(")
    elif(decision == "yes"):
        print("Awesome!")
    else:
        print("Answer again.")


#2

@linhy1,
In your WHILE-loop condition you are referring to the variable decision which you have not defined....

But there is also the problem of the usage of the comparison operator and the logical operator' !!
If you use the code

print("##===============================================")
print('##==>> decision != "yes" or decision != "no"')
print("##===============================================")
decision=""
print("decision[{}] \t==> {}".format(
    decision,decision != "yes" or decision != "no"))
decision="yes"
print("decision[{}] \t==> {}".format(
    decision,decision != "yes" or decision != "no"))
decision="no"
print("decision[{}] \t==> {}".format(
    decision,decision != "yes" or decision != "no"))
decision="x"
print("decision[{}] \t==> {}".format(
    decision,decision != "yes" or decision != "no"))
print("##===============================================")
print('##==>> decision == "yes" or decision == "no"')
print("##===============================================")
decision=""
print("decision[{}] \t==> {}".format(
    decision,decision == "yes" or decision == "no"))
decision="yes"
print("decision[{}] \t==> {}".format(
    decision,decision == "yes" or decision == "no"))
decision="no"
print("decision[{}] \t==> {}".format(
    decision,decision == "yes" or decision == "no"))
decision="x"
print("decision[{}] \t==> {}".format(
    decision,decision == "yes" or decision == "no"))
print("##===============================================")
print('##==>> not (decision == "yes" or decision == "no")')
print("##===============================================")
decision=""
print("decision[{}] \t==> {}".format(
    decision,not (decision == "yes" or decision == "no")))
decision="yes"
print("decision[{}] \t==> {}".format(
    decision,not (decision == "yes" or decision == "no")))
decision="no"
print("decision[{}] \t==> {}".format(
    decision,not (decision == "yes" or decision == "no")))
decision="x"
print("decision[{}] \t==> {}".format(
    decision,not (decision == "yes" or decision == "no")))

you would get the OUTPUT

##===============================================
##==>> decision != "yes" or decision != "no"
##===============================================
decision[] 	==> True
decision[yes] 	==> True
decision[no] 	==> True
decision[x] 	==> True
##===============================================
##==>> decision == "yes" or decision == "no"
##===============================================
decision[] 	==> False
decision[yes] 	==> True
decision[no] 	==> True
decision[x] 	==> False
##===============================================
##==>> not (decision == "yes" or decision == "no")
##===============================================
decision[] 	==> True
decision[yes] 	==> False
decision[no] 	==> False
decision[x] 	==> True

and you would see
that the code you are using is creating an infinite-loop as you are never exiting !!!!

http://stackoverflow.com/questions/14594522/how-to-break-out-of-while-loop-in-python
http://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops-in-python
http://stackoverflow.com/questions/13962471/python-break-function-doesnt-end-while-true


#3

Completely unreadable. Rewrite to:

while not ( ... ):

(Avoid multiple NOT's in a condition, it doesn't need to be difficult to read.)


#5

How do you make it uninfinite?


#6

Loops stop when their conditions are no longer satisfied.

If your loop does not stop, then your condition does not get satisfied.

So change the condition, or change what the condition tests.


#7

@linhy1,
apart of what @ionatan wrote
you could make use of the break statement
like

while True:
    decision = input("You can still do it, though!  Do you keep going?")
    
    if(decision == "no"):
        print("Good bye. =(")
        break
    elif(decision == "yes"):
        print("Awesome!")
        break
    else:
        print("Answer again.")

#8

How do you get it to continue for yes?


#9

@linhy1,
Q:How do you get it to continue for yes?
A: Remove the break-statement from the ELIF


#10

Thank you, I'll try that! :slight_smile: