尝试将第一个while循环嵌套在第二个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__:块中,这样它只在直接执行时才执行。在
例如:
^{pr2}$
最后
以上就是时尚苗条最近收集整理的关于python怎么回到上一步循环_如何返回上一个while循环?的全部内容,更多相关python怎么回到上一步循环_如何返回上一个while循环内容请搜索靠谱客的其他文章。
发表评论 取消回复