问题
This is my code :
#Choose Report
def chooseReport():
print "Choose a report."
while True:
choice = raw_input("Enter A or B: ")
if choice == 'a' or choice == 'A':
reportA()
break
elif choice == 'b' or choice == 'B':
reportB()
break
else:
continue
When I input either a
or b
, it just asks me to "Enter A or B"
again. It doesn't go to the functions its supposed to.
Any idea why is this?
回答1:
The code is perfect, except a redundant else, as mentioned in the comment. Are you entering a
(a + space) rather than simply a
(a without space) ? The problem is in the input that you are providing and not in the code!
回答2:
def chooseReport():
print "Choose a report."
t=True # t starts at true
while t:
choice = raw_input("Enter A or B: ")
if choice == 'a' or choice == 'A':
reportA()
t=False # t turns false to break out of loop
elif choice == 'b' or choice == 'B':
reportB()
t=False
Try this. It keeps looping when t is true and stops when t is false. The problem might also be in reportA or reportB or how you are calling chooseReport.
回答3:
The problem is in the raw_input()
. It returns a string but maybe this string is "a "
or "a\n"
though you have entered "a"
or "b"
.
I would do this:
def chooseReport():
print "Choose a report."
while True:
choice = raw_input("Enter A or B: ")
if "a" in choice or "A" in choice:
reportA()
break
elif "b" in choice or "B" in choice:
reportB()
break
else:
continue
回答4:
Tried your code in the following script, it works fine both on Linux and on Windows.
def reportA():
print "AAAAA"
def reportB():
print "BBBBB"
#Choose Report
def chooseReport():
print "Choose a report."
while True:
choice = raw_input("Enter A or B: ")
if choice == 'a' or choice == 'A':
reportA()
break
elif choice == 'b' or choice == 'B':
reportB()
break
else:
continue
chooseReport();
回答5:
First, your code works fine, the most probably error is that you are writing a wrong input (e.g: with more characters). To solve that you could use "a" in choice or "A" in choice
. But if it isn't working... keep reading.
It's seems that break
isn't affecting the while loop
, I don't have python 2 so I am not very sure why (in python 3 [after change raw_input
to input
and print
to print()
] your code works perfect). So you should use the condition of the while
to break it.
while True
work theorically for ever because each time the code is executed it checks the condition -True
- and because it's true it keeps looping.
You could manipulate that condition in order to break the loop (don't allow execute again its code).
For example you could use this:
#Choose Report
def chooseReport():
print "Choose a report."
allow = True # allow start as True so the while will work
while allow:
choice = raw_input("Enter A or B: ")
if choice.lower().strip() == "a": # This is better. .lower() make "A" > "a", and .strip() delete " a " to "a", and "a/n" to "a".
reportA()
allow = False # allow now is False, the while won't execute again
elif choice.lower().strip() == "b":
reportB()
allow = False # allow now is False, the while won't execute again
# The else is complete redundant, you don't need it
回答6:
Code is fine. I think you call your chooseReport() function in a loop or your input has extra characters and if conditions didn't satisfied.
来源:https://stackoverflow.com/questions/48358686/while-loop-not-breaking-using-python