如何返回上一個 while 循環? (How do I return to the previous while loop?)


問題描述

如何返回上一個 while 循環? (How do I return to the previous while loop?)

    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 循環。有什麼幫助嗎?謝謝!


參考解法

方法 1:

It looks like your second loop should actually be an outer loop (in this case often called a "game loop"). The entire application loops until the user specifies to end the application. Something like this:

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

(Note: This is freehand code and I'm not entirely familiar with Python, so there may need to be some tweaking on the specifics. This is meant to demonstrate the structure, not to be copied/pasted as production code.)

方法 2:

Try nesting the first while loop inside of the second. It'll run your calculation code first, check to see if you'd like to do another, and then return to the top of the while True: loop to do another calculation.

Like this:

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

Hope that helps

Depending on what you're trying to do, and where this particular code appears in your program, it's usually advised to wrap your code inside of functions. That way, it doesn't automatically run when you import your module. You have to call a function to make the code run.

If you want to make this an executable script, you'd want to wrap the main loop code in a if __name__ == '__main__: block so that it only executes if it's being executed directly.

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

(by stevo043Davidnoseworthy)

參考文件

  1. How do I return to the previous while loop? (CC BY‑SA 2.5/3.0/4.0)

#continue #Python #while-loop






相關問題

skrip shell: loop bersarang dan lanjutkan (shell script : nested loop and continue)

syntaxError:“繼續”在循環中不正確 (syntaxError: 'continue' not properly in loop)

php continue - 替代方式? (php continue - alternative way?)

Python 中的歐拉 #4 (Euler #4 in Python)

if-continue vs 嵌套 if (if-continue vs nested if)

如何返回上一個 while 循環? (How do I return to the previous while loop?)

php foreach 繼續 (php foreach continue)

在while循環中繼續的奇怪行為 (weird behaviour of continue in while loop)

是否可以使用 'yield' 來生成 'Iterator' 而不是 Scala 中的列表? (Is it possible to use 'yield' to generate 'Iterator' instead of a list in Scala?)

BASH:在 for 循環中使用 continue (BASH: Using a continue in a for loop)

rand() 函數後繼續指令的問題 (Problem with continue instruction after rand() function)

在tictactoe遊戲中重複輸入錯誤 (Error in repeating input in a tictactoe game)







留言討論