2017-02-19 44 views
1

美好的一天,我在作爲一個學校作業正在做這個程序的問題。基本上,菜單功能顯示爲我想要的,但它不會正確執行任何其他功能。所以如果我輸入一個數字,它只是循環回菜單。即使退出選項也不起作用。我真的不明白爲什麼。它似乎完全遵循我所有示例代碼的語法。Python菜單功能不能正常工作

def Menu(): 
    print ("") 
    print ("CALCULATIONS MENU") 
    print ("1) AREA (SQUARE)") 
    print ("2) AREA (RECTANGLE)") 
    print ("3) AREA (CIRCLE)") 
    print ("4) PERIMETER (SQUARE)") 
    print ("5) PERIMETER (RECTANGLE)") 
    print ("6) PERIMETER (CIRCLE)") 
    print ("7) EXIT") 
    Test = input ("Input menu option(1-7): ") 
    return Test 

def ASQ(Height): 
    print ("") 
    print ("The area of the square is:", Height * Height) 

def AREC(Height, Width): 
    print ("") 
    print ("The area of the rectangle is:", Height * Width) 

def ACIR(Radius): 
    print ("") 
    print ("The area of the circle is:", 3.1415 * Radius**2) 

def PSQ(Height): 
    print ("") 
    print ("The perimeter of the square is:", Height * 4) 

def PREC(Height, Width): 
    print ("") 
    print ("The perimeter of the rectangle is:", Width*2 + Height*2) 

def PCIR(Diameter): 
    print ("") 
    print ("The perimeter of the circle is:", Diameter * 3.1415) 

Loop = 1 
Selection = 0 
while Loop == 1: 
    Selection = Menu() 
    if Selection == 1: 
     ASQ(input("Enter the length of one side: ")) 
    elif Selection == 2: 
     AREC(input("Enter height: "), input("Enter width: ")) 
    elif Selection == 3: 
     ACIR(input("Enter radius: ")) 
    elif Selection == 4: 
     PSQ(input("Enter the length of one side: ")) 
    elif Selection == 5: 
     PREC(input("Enter height: "), input("Enter width: ")) 
    elif Selection == 6: 
     PCIR(input("Enter diameter: ")) 
    elif Selection == 7: 
     Loop = 0 

print ("Good bye") 
+2

'Test = int(input(「Input menu option(1-7):」))'else'Test'是一個字符串,並不與您的整數進行比較。 –

+0

謝謝。奇怪的是,它們使用的示例代碼都沒有在類似的情況下定義int。它當然有幫助。現在出現了新的錯誤,但我認爲我走在了正確的道路上。 – nodeg

+0

Python 2:會工作 –

回答

0

問題:

Test = input ("Input menu option(1-7): ") 

Test將包含用戶輸入作爲string,所以當你保存的Test的價值Selection和比較它在if-statements值,

if Selection == 1: 

您正在比較intstring這是不會工作。

解決方案:

通過更改

Test = input("Input menu option(1-7): ") 

用戶輸入轉換爲int

Test = int(input ("Input menu option(1-7): ")) 

或 包括號碼if-statements在雙引號""

if Selection == "1":