简体   繁体   English

“ while循环”不中断(使用Python)

[英]“while loop” not breaking (using Python)

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. 当我输入ab ,它只是要求我再次"Enter A or B" It doesn't go to the functions its supposed to. 它不会执行应有的功能。

Any idea why is this? 知道为什么吗?

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. 当t为真时,它将保持循环,而当t为假时,它将停止。 The problem might also be in reportA or reportB or how you are calling chooseReport. 问题也可能出在reportA或reportB或您如何调用choiceReport。

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) ? 你输入a (A +空格),而不是简单的a (没有空间)? The problem is in the input that you are providing and not in the code! 问题出在您提供的输入中,而不是代码中!

First, your code works fine, the most probably error is that you are writing a wrong input (eg: with more characters). 首先,您的代码运行良好,最有可能的错误是您输入了错误的输入(例如:包含更多字符)。 To solve that you could use "a" in choice or "A" in choice . 要解决此问题,可以"a" in choice or "A" in choice使用"a" in choice or "A" in choice使用"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). 看来break并没有影响while loop ,我没有python 2,所以我不太确定为什么(在python 3中[将raw_input更改为inputprintprint()您的代码可以正常工作)。 So you should use the condition of the while to break it. 因此,您应该使用while条件打破它。

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. while True在理论上永远有效,因为每次执行代码时,它都会检查条件True并且因为它是真的,所以它会不断循环。
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

The problem is in the raw_input() . 问题出在raw_input() It returns a string but maybe this string is "a " or "a\\n" though you have entered "a" or "b" . 它返回一个字符串,但是尽管您输入了"a""b"但该字符串可能是"a ""a\\n" "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

Tried your code in the following script, it works fine both on Linux and on Windows. 在下面的脚本中尝试了代码,它在Linux和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();

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. 我认为您可以循环调用您的choiceReport()函数,或者您的输入包含多余的字符,并且条件不满足。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM