下面的代码在while循环中显示了我的try和except语句,它在第一部分中运行良好,但是由于某种原因,它不会继续进行到下一个循环,在该循环中我要求用户选择一个数学运算符,然后代码从头开始,然后再次重复整个过程。
while True:
try:
num1=float(input("Please enter a value in Number form, with 5 decimal places max"))
num2=float(input("And your second value in Number form, with 5 decimal places max"))
break
except ValueError:
print("Incorrect, try again")
while True:
try:
userOp=input("Press 1 for Addition, 2 for Subtraction, 3 for Multiplication and 4 for Division")
break
当代码运行时,它要求用户输入一个可以按预期工作的值,在输入两个值之后,代码将继续执行,但不会继续到第二个while循环,而是继续到代码的开头。我如何从第一个循环“退出”并进入下一个循环?
答案 0 :(得分:2)
这将解决您的问题
while True:
try:
num1 = float(input("Please enter a value in Number form, with 5 decimal places max"))
num2 = float(input("And your second value in Number form, with 5 decimal places max"))
break
except ValueError:
print("Incorrect, try again")
while True:
try:
userOp = int(input("Press 1 for Addition, 2 for Subtraction, 3 for Multiplication and 4 for Division"))
break
except ValueError:
print('Try again')