如何返回上一个while循环?

时间:2015-12-04 18:45:59

标签: python while-loop continue

    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”为“你想进行另一次计算吗?”,我试图让我的程序回到顶部循环。到目前为止,我所拥有的东西让我回到底部循环。有帮助吗?谢谢!

2 个答案:

答案 0 :(得分:0)

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

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并不完全熟悉,因此可能需要对细节进行一些调整。这是为了演示结构,而不是作为生产代码复制/粘贴。 )

答案 1 :(得分:0)

尝试在第二个内嵌第一个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__:块中,这样只有在它被直接执行时才会执行。

e.g:

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()