【发布时间】:2014-09-15 17:55:24
【问题描述】:
这是我写的一个简单的计算器,但完成后它不会重新启动应用程序 这是我的代码:
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
print("Select from the list bellow which oporation you want the calculator to do.")
print("A.Add")
print("S.Subtract")
print("M.Multiply")
print("D.Divide")
choice = input("Enter choice(a/s/m/d):")
if choice != 'a' and choice != 's' and choice != 'm' and choice != 'd':
print (" the letter you intered is not in our lists!")
num1 = int(input("Enter an interger as your first number: "))
num2 = int(input("Enter an integer as second number: "))
if choice == 'a':
print(num1,"+",num2,"=", add(num1,num2))
elif choice == 's':
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == 'm':
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == 'd':
print(num1,"/",num2,"=", divide(num1,num2))
else:
print("Invalid input")
input("press enter to close")
当它完成时,我希望它询问用户是否要重新启动。我在循环它不起作用时使用了不同的方法。
【问题讨论】:
-
请向我们展示您尝试过的 while 循环,以便我们帮助您了解为什么它不起作用。
-
你的缩进搞砸了..
-
您的问题显示 no
while循环,因此无法说明您假设的循环为什么不起作用。 -
if choice not in {"a","s","m","d"}可以替换你的长 if 语句 -
这是我使用的 while 循环之一:'code' while True: restart = input("type 1 to play and 2 for no :") if restart==1: main() else: print("感谢您的参与") break @WinstonEwert
标签: python restart python-3.4