How To Kill A Program In Python
I'm trying to kill a program in python 3 when the user says 'no' to start a maths quiz, : here is the code I'm using import sys while True: new_item = input('> ') if ne
Solution 1:
Your problem is here:
if new_item == "Yes" or "yes":
You need to use either:
if new_item in ["Yes", "yes"]:
or:
if new_item == "Yes" or new_item == "yes"
Your original code is parsed as:
if (new_item == "Yes") or "yes":
and this always evaluates to True
since "yes"
is a true value.
Solution 2:
if new_item == "Yes" or "yes":
This conditional is always True
. It may be stated as:
(new_item == "Yes") or ("yes")
Non-empty string 'yes'
is always evaluated to True
.
Change conditional to:
if new_item in ['Yes', 'yes']:
Solution 3:
You need to change your if statement, it isn't evaluating properly. You need to use this code to fix your problem:
import sys
while True:
new_item = input("> ")
if new_item == "Yes" or new_item == "yes":
break
elif new_item == "no":
sys.exit()
Solution 4:
What about
import sys
while True:
new_item = input("> ")
new_item = new_item.lower()
#everything you wrote in input will be lowercase, no more "or" problems
if new_item.lower() == "yes":
break
elif new_item.lower() == "no":
sys.exit()
Post a Comment for "How To Kill A Program In Python"