問題描述
如何返回上一個 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 stevo043、David、noseworthy)