2017-02-13 37 views
0

我對Python非常陌生,所以我提前致歉!我正在嘗試創建一個列表,用戶可以選擇添加數字,顯示平均數,顯示中位數,按順序打印列表,按相反順序打印列表或退出。我以爲我在正確的軌道上,但我不能讓它運行。誰能幫我?Python中的菜單列表

def main(): 
    myList = [ ] 
    addOne(myList) 
    choice = displayMenu() 
    while choice != '6': 
     if choice == '1': 
      addOne(myList) 
     elif choice == '2': 
      mean(myList) 
     elif choice == '3': 
      median(myList) 
     elif choice == '4': 
      print(myList) 
     elif choice == '5': 
      print(myList) 
     choice = displayMenu() 

    print ("\nThanks for playing!\n\n") 

def displayMenu(): 
    myChoice = '0' 
    while myChoice != '1' and myChoice != '2' \ 
       and myChoice != '3' \ 
       and myChoice != '4' and myChoice != '5': 
     print("""\n\nPlease choose 
       1. Add a number to the list/array 
       2. Display the mean 
       3. Display the median 
       4. Print the list/array to the screen 
       5. Print the list/array in reverse order 
       6. Quit 
       """) 
     myChoice = input("Enter option---> ") 
     if myChoice != '1' and myChoice != '2' and \ 
      myChoice != '3' and myChoice != '4' and myChoice != '5': 
      print("Invalid option. Please select again.") 

    return myChoice 

#This should make sure that the user puts in a correct input 
def getNum(): 
    num = -1 
    while num < 0: 
     num = int(input("\n\nEnter a non-negative integer: ")) 
     if num < 0: 
      print("Invalid value. Please re-enter.") 

    return num 

#This is to take care of number one on the list: Add number 
def addOne(theList): 
    while True: 
     try: 
      num = (int(input("Give me a number:"))) 
      num = int(num) 
      if num < 0: 
       raise exception 
      print("Thank you!") 
      break 
     except: 
      print("Invalid. Try again...") 
     theList.append(num) 


#This should take care of the second on the list: Mean  
def mean(theList): 
    myList = [] 
    listSum = sum(myList) 
    listLength = len(myList) 
    listMean = listSum/listLength 
    print("The mean is", listMean) 

#This will take care of number three on the list: Median 
def median(theList): 
    myList = [] 
    return myList.median(theList.array(myList)) 
    print("The median is", listMedian) 


#This will take care of the fourth thing on the list: Print the list 
def sort(theList): 
    theList.sort() 
    print(theList) 

#This will take care of the fifth thing on the list 
def reversesort(theList): 
    theList.sort(reverse=True) 
    print(theList) 


main() 

當我儘量選擇選項2它給了我:

Traceback (most recent call last): 
    File "/Users/Gunter/Documents/CS 110/List and Traversal and Exception  Handling.py", line 94, in <module> 
    main() 
    File "/Users/Gunter/Documents/CS 110/List and Traversal and Exception Handling.py", line 12, in main 
    mean(myList) 
    File "/Users/Gunter/Documents/CS 110/List and Traversal and Exception Handling.py", line 73, in mean 
    listMean = listSum/listLength 
ZeroDivisionError: division by zero 

當我嘗試運行第三個選項它給了我:

Traceback (most recent call last): 
    File "/Users/Gunter/Documents/CS 110/List and Traversal and Exception Handling.py", line 94, in <module> 
    main() 
    File "/Users/Gunter/Documents/CS 110/List and Traversal and Exception Handling.py", line 14, in main 
    median(myList) 
    File "/Users/Gunter/Documents/CS 110/List and Traversal and Exception Handling.py", line 79, in median 
    return myList.median(theList.array(myList)) 
AttributeError: 'list' object has no attribute 'median' 
+0

你需要比「我無法讓它運行」更具體。 –

+0

我試過複製/粘貼這個,還有一些未定義的東西。你有什麼問題?這應該是一個獨立的例子嗎? – roganjosh

+0

如果我嘗試選擇計算平均值或計算平均值的選項,它會爆炸。如果我嘗試選擇該選項,則按順序或反向打印列表,只顯示一個空集。 –

回答

1

問題,平均

您從回溯消息中得到了非常好的提示:ZeroDivisionError: division by zero。你們部門的分母是什麼? listLengthlistLength從哪裏計算?用本地範圍創建的空列表(myList)的長度。您需要將該列表的長度作爲參數傳遞給函數。

你的平均值計算還存在另一個問題:你的分子不正確的分裂。修復它是作爲練習留給你的。

問題與中位數

再次,回溯消息給你一個很好的提示:AttributeError: 'list' object has no attribute 'median'common sequence operationsList-type operations都沒有提供median方法。您需要將其定義爲函數,或者構建一個將其定義爲類方法的類,並將List作爲內部存儲。或者,您可以避免重新發明車輪 - 可能是所有Pythonic答案最多的 - 並使用標準庫中的statistics模塊。