【问题标题】:How do I return to the previous while loop?如何返回上一个 while 循环?
【发布时间】:2015-12-04 18:45:59
【问题描述】:
    x, y = raw_input("Enter 2 numbers separated by a space.").split()
answer = 0
#Enter the 2 numbers to be calculated.
print "You have selected, A = "+ x + " and B = " + y + "."
while int(y):
    if (not int(y) % 2 == 0):
        # this checks if y is even or odd
        answer = int(answer) + int(x)  
        print "A = " + str(x) + " and B = " + str(y) + "."
        print "B is odd so we'll add A to the total."
        print "The running total is " + str(answer) + "."
    else: (int(y) % 2 == 0)
    print "A = " + str(x) + " and B = " + str(y) + "."
    print "B is even, so we'll ignore that number."

    x = int(x) * 2
    y = int(y) / 2

print "The product is " + str(answer) + "."

while True:

    a = raw_input("Would you like to make another calculation? Y or N")
    if str(a) == "Y" or str(a) == "y":
        continue
    if str(a) == "N" or str(a) == "n":
        print "Thank you have a nice day!"
        break
    else:
        print "Invalid entry. Ending program."
        break

如果输入“Y”或“y”来表示“您要进行另一个计算吗?”,我正试图让我的程序返回到顶部 while 循环。到目前为止,我所拥有的让我回到了底部的 while 循环。有什么帮助吗?谢谢!

【问题讨论】:

    标签: python while-loop continue


    【解决方案1】:

    看起来您的第二个循环实际上应该是一个外部循环(在这种情况下通常称为“游戏循环”)。 整个应用程序循环直到用户指定结束应用程序。像这样的:

    a = "Y"
    while (a == "Y"):
    
        ##########
        # All of your "first loop" logic goes here.
        # This is the primary "step" of any given instance of the "game".
        ##########
    
        # Then, prompt the user to continue or not...
        a = raw_input("Would you like to make another calculation? Y or N")
        if str(a) == "Y" or str(a) == "y":
            continue
        if str(a) == "N" or str(a) == "n":
            print "Thank you have a nice day!"
            break
        else:
            print "Invalid entry. Ending program."
            break
    

    (注意:这是手写代码,我对 Python 并不完全熟悉,因此可能需要对细节进行一些调整。这是为了演示结构,而不是作为生产代码复制/粘贴。 )

    【讨论】:

    • 感谢大家的帮助,但我尝试了两种方法,仍然没有运气。相反,我得到了 x 和 y 的自动值。 6400 和 0 是我的总数。它甚至没有要求输入数字,并且在询问我是否要进行另一次计算之后,它重复了这个问题,除非我输入“n”。非常感谢您花时间查看我的代码!
    【解决方案2】:

    尝试将第一个 while 循环嵌套在第二个循环中。它会首先运行您的计算代码,检查您是否要执行另一个计算,然后返回到while True: 循环的顶部进行另一个计算。

    像这样:

    while True:
        x, y = raw_input("Enter 2 numbers separated by a space.").split()
        answer = 0
        #Enter the 2 numbers to be calculated.
        print "You have selected, A = "+ x + " and B = " + y + "."
        while int(y):
            if (not int(y) % 2 == 0):
                # this checks if y is even or odd
                answer = int(answer) + int(x)  
                print "A = " + str(x) + " and B = " + str(y) + "."
                print "B is odd so we'll add A to the total."
                print "The running total is " + str(answer) + "."
            else: (int(y) % 2 == 0)
            print "A = " + str(x) + " and B = " + str(y) + "."
            print "B is even, so we'll ignore that number."
    
            x = int(x) * 2
            y = int(y) / 2
    
        print "The product is " + str(answer) + "."
    
        a = raw_input("Would you like to make another calculation? Y or N")
        if str(a) == "Y" or str(a) == "y":
            continue
        if str(a) == "N" or str(a) == "n":
            print "Thank you have a nice day!"
            break
        else:
            print "Invalid entry. Ending program."
            break
    

    希望有帮助

    根据您要执行的操作以及此特定代码在程序中出现的位置,通常建议将您的代码包装在函数中。这样,它不会在您导入模块时自动运行。您必须调用一个函数才能使代码运行。

    如果您想让它成为一个可执行脚本,您需要将主循环代码包装在 if __name__ == '__main__: 块中,以便它仅在直接执行时才执行。

    例如:

    def perform_calculation():
        while True:
            x, y = raw_input("Enter 2 numbers separated by a space.").split()
            answer = 0
            #Enter the 2 numbers to be calculated.
            print "You have selected, A = "+ x + " and B = " + y + "."
            while int(y):
                if (not int(y) % 2 == 0):
                    # this checks if y is even or odd
                    answer = int(answer) + int(x)  
                    print "A = " + str(x) + " and B = " + str(y) + "."
                    print "B is odd so we'll add A to the total."
                    print "The running total is " + str(answer) + "."
                else: (int(y) % 2 == 0)
                print "A = " + str(x) + " and B = " + str(y) + "."
                print "B is even, so we'll ignore that number."
                x = int(x) * 2
                y = int(y) / 2
            print "The product is " + str(answer) + "."
    
    def run_loop():
        while True:
            perform_calculation()
            a = raw_input("Would you like to make another calculation? Y or N")
            if str(a) == "Y" or str(a) == "y":
                continue
            if str(a) == "N" or str(a) == "n":
                print "Thank you have a nice day!"
                break
            else:
                print "Invalid entry. Ending program."
                break
    
    if __name__ == '__main__':
        run_loop()
    

    【讨论】: