我无法让python3创建的程序停止运行我的循环。我做错什么了?我需要能够返回到主菜单后,输出每个功能。你知道吗
救命啊!你知道吗
# define functions
def add(x, y):
"""This function adds two numbers"""
return x + y
def subtract(x, y):
"""This function subtracts two numbers"""
return x - y
def multiply(x, y):
"""This function multiplies two numbers"""
return x * y
def divide(x, y):
"""This function divides two numbers"""
return x / y
# take input from the user
loop = 1
while loop ==1:
print ("Hi Prof. Shah! Welcome to my Project 3 calculator!")
print("Please select an operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
print("5.Remainder")
print("6.Exit")
choice = input("Enter choice(1/2/3/4/5/6):")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == '3':
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
print(num1,"/",num2,"=", divide(num1,num2))
elif choice == '5':
print(num1,"%",num2,"=", remainder(num1,num2))
elif choice == '6':
print("Goodbye, Prof. Shah!")
事先谢谢你的帮助。你知道吗
您没有更改
loop
的值,而是导致了无限循环。你知道吗这里有一个建议:
您需要在每次“打印”之后添加“break”,以便while循环可以在打印信息之后中断。例如:
您只能用表达式
loop = 1
设置loop
的值。在稍后的某个时候,您需要将其更改为1以外的值才能退出循环,因为条件是loop == 1
。你知道吗在循环的适当点将
loop
的值设置为1以外的值。你知道吗相关问题 更多 >
编程相关推荐