我無法讓我的Python 3創建的程序停止運行我的循環。我究竟做錯了什麼?輸出每個功能後,我需要能夠返回到主菜單。無法計算Python 3程序
幫助!
# 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!")
在此先感謝您的幫助。
請妥善縮進代碼,否則我們無法知道其實際作用,因爲它是Python的方式。 – Amber 2014-12-08 01:16:20
[Python 3程序中無效的Elif語法錯誤]的可能重複(http://stackoverflow.com/questions/27347442/invalid-elif-syntax-error-in-python-3-program) – kay 2014-12-08 01:17:45
我試圖切割和粘貼我的代碼直接來自Python 3,但我一直在Stackoverflow上收到錯誤消息。有另一種方法嗎? – CherylR 2014-12-08 01:19:46