Python Try&;除环

Python Try&;除环,python,python-2.7,loops,Python,Python 2.7,Loops,因此,我试图在python上制作一个计算器,但遇到一个错误,它说“continue”在循环中不正确” 代码如下: try: num1m = int(input("Select the first number --> ")) except ValueError: print("That's no number!") continue try: num2m = int(input("Select the second number --> ")) except Valu

因此,我试图在python上制作一个计算器,但遇到一个错误,它说“continue”在循环中不正确”

代码如下:

try:
  num1m = int(input("Select the first number --> "))
except ValueError:
  print("That's no number!")
  continue
try:
  num2m = int(input("Select the second number --> "))
except ValueError:
  print("That's no number!")
  continue
num3m = (num1m * num2m)
str(num3m)
print("The sum is " + num3m + ".")
有人能帮我吗,谢谢:)

你不能在任何地方都使用,有语法可以遵循:

continue
只能在语法上嵌套在
for
while
循环中,而不能嵌套在该循环中的函数或类定义或
finally
子句中。它将继续最近的封闭循环的下一个循环


如前所述,
continue
必须在循环中使用。一种让代码正常工作的方法

while True:
    try:
        num1m = int(input("Select the first number --> "))
    except ValueError:
        print("That's no number!")
        continue
    try:
        num2m = int(input("Select the second number --> "))
    except ValueError:
        print("That's no number!")
        continue
    break
num3m = (num1m * num2m)
str(num3m)
print("The sum is " + str(num3m) + ".") # make sure to convert int to str

Continue是loop do do next iteration的关键字。如果您试图使用“Continue”作为命令返回几行并要求用户重新输入他们的号码,这可能对您很有用:LokiH如果有任何答案解决了您的问题,请单击旁边(左侧)的绿色复选标记接受它,这将有助于社区。非常感谢。