2014-12-08 44 views
-1

我無法讓我的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!") 

在此先感謝您的幫助。

+2

請妥善縮進代碼,否則我們無法知道其實際作用,因爲它是Python的方式。 – Amber 2014-12-08 01:16:20

+0

[Python 3程序中無效的Elif語法錯誤]的可能重複(http://stackoverflow.com/questions/27347442/invalid-elif-syntax-error-in-python-3-program) – kay 2014-12-08 01:17:45

+0

我試圖切割和粘貼我的代碼直接來自Python 3,但我一直在Stackoverflow上收到錯誤消息。有另一種方法嗎? – CherylR 2014-12-08 01:19:46

回答

1

您只能將loop的值設置爲loop = 1。在稍後的某個時間點,您需要將其更改爲1以外的值以退出循環,因爲條件爲loop == 1

在循環中的適當位置將值loop設置爲1以外的值。

+0

這是對問題的正確診斷。請注意,像這樣循環的更多Pythonic方法通常是使循環故意無限,使用'while True',然後使用'break'退出循環。雖然有些程序員不喜歡這種風格(他們不喜歡'break',因爲它是'goto'的一種形式),所以請謹慎使用它。 – Blckknght 2014-12-08 01:21:27

+0

正確。我的教授不希望我們爲這個項目使用休息。 – CherylR 2014-12-08 01:22:50

+0

我試圖添加這個,但我不斷收到縮進錯誤。 :elif choice =='6': loop = 0 print(「再見,Shah教授!」) – CherylR 2014-12-08 01:25:06

0

您需要在每次「打印」後添加「break」,以便在打印信息後while循環可以中斷。例如:

if choice == '1': 
    print(num1,"+",num2,"=", add(num1,num2)) 
    break 
elif choice == '2': 
    print(num1,"-",num2,"=", subtract(num1,num2)) 
    break 
+0

這是否會在每次打印後將我返回到主菜單? – CherylR 2014-12-08 01:24:31

0

你不改變的loop的價值,它是造成無限循環。

這裏有一個建議:

flag = True 
while flag: 
    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 = int(input("Enter choice(1/2/3/4/5/6):")) #dangeeer!! 
    if choice >= 1 and choice < 6: #here you keep inside the loop. 
     x = float(input("First value:")) #dangeeer!! 
     y = float(input("Second value:")) #dangeeer!! 
     if choice == 1 : print(add(x, y)) 
     elif choice == 2: print(subtract(x, y)) 
     elif choice == 3: print(multiply(x, y)) 
     elif choice == 4: print(divide(x, y)) 
     elif choice == 5: print(remainder(x, y)) 
    elif choice == 6: #here you get out of the loop 
     print("Goodbye, Prof. Shah!") 
     flag = False 
    else: 
     print("Choose a number between 1 and 6") 
+0

當我在您的建議後添加了以下代碼時,我收到了無效的語法消息:elif == 6:print(Goodbye,Shah!教授) – CherylR 2014-12-08 01:40:50

+0

put'elif choice == 6:flag = False' – 2014-12-08 01:41:49

+0

正確... I添加了這個: 其他:標誌=假 ELIF選擇== 6:打印(再見,沙阿教授!) – CherylR 2014-12-08 01:47:40