由于某种原因,我的
while
循环不会中断
我尝试通过使用数字来控制循环等来修改循环,但它在我当前的代码中不起作用:
escape = False #Initialize escape variable
while not escape:
try:
num1 = float(input("Enter the first number: "))
oppChoice = input("Enter operation (+, -, *, /): ")
num2 = float(input("Enter the second number: "))
#Call the operations method with the numbers entered
calc_instance.operations(num1, num2)
#Using an if-elif statement to guide the print statements
if oppChoice == "+":
print(f"Result (Addition): {calc_instance.add}")
elif oppChoice == "-":
print(f"Result (Subtraction): {calc_instance.subtract}")
elif oppChoice == "*":
print(f"Result (Multiplication): {calc_instance.multiply}")
elif oppChoice == "/":
print(f"Result (Division): {calc_instance.divide}")
else:
print("Invalid operation choice. Please enter +, -, *, or /.")
#Asking if the user wants to continue calculating
end_program = input("Are you done calculating? (y/n)").lower()
if end_program in ("y", "yes"):
escape = True #Set escape to True to break the loop
elif end_program not in ("n", "no"):
print("Please enter yes or no.")
except ValueError: #Handle invalid input
print("Invalid input. Please enter valid numeric values.")
将escape设置为true后,再突破有没有坏处? 只需在 escape = True 部分下输入break