2017-05-12 69 views
0

我遇到了輸出我有一堆函數的結果的問題。任何信息都非常感謝,因爲我已經堅持了幾個小時。輸出列表時出錯

錯誤:

minNum = theArray[0] 
IndexError: list index out of range 

所需的輸出:

The original array was: 
[22, 49, 80, 4, 53, 31, 6, 30, 61, 87] 
The maximum value is: 87 
The minimum value is: 4 
The total value is: 423 
The average value is: 42.3 
The sorted array is: 
[4, 6, 22, 30, 31, 49, 53, 61, 80, 87] 

功能: randIntArray - 填充隨機整數(帶測試)的陣列。 getInt(提示) - 嘗試從用戶10x獲得一個整數。 getData() - 使用getInt將整數存儲到數組(userIntList)中執行循環10x。 交換 - 列表的交換例程。 sortArray - 使用交換例程進行排序。 displayArray - 打印數組。 maxValue - 在列表中查找最大值(不使用函數中內置的python)。 minValue - 在列表中查找min(不使用函數中內置的python)。 aveValue - 在列表中查找ave#(不使用函數中內置的python)。 totalValue - 查找列表中整數的整數(不使用構建函數的python) dataOut(theArray) - 打印上面列出的值。 processInput(theArray) - 使用dataOut,排序並以未排序和排序的形式提示列表。 unitTest - 使用隨機整數函數來測試和打印結果。

import random 

def randIntArray(): 
    randomList = [] 
    randomList = random.sample(range(1, 101), 10) 
    return randomList 

def getInt(prompt): 
    # Function to call when asking for the A and B values later. 
    try: 
     x = input(prompt) 
     y = int(x) 
     return y 
    except: 
     print("That was not an integer. Please enter an integer. ") 


def getData(): 
    userIntList = [] 
    for i in range(10): 
     userNum = getInt("Please enter value number : ") 
     userIntList.append(userNum) 
    return userIntList 


def swap(a, j, k): 
    temp1 = a[j] 
    a[j] = a[k] 
    a[k] = temp1 

def sortArray(a): 
    for i in range(len(a)): 
     for k in range(len(a) - 1): 
      first = k 
      second = k + 1 

      if (a[first] > a[second]): 
       # Uses Swap function 
       swap(a, first, second) 

def displayArray(theArray): 
    outList = theArray 
    return outList 

def maxValue(theArray): 
    maxNum = 0 
    for i in theArray: 
     if i > maxNum: 
      maxNum = i 
    return maxNum 

def minValue(theArray): 
    minNum = theArray[0] 
    for i in theArray: 
     if i < minNum: 
      minNum = i 
    return minNum 

def totalValue(theArray): 
    aTotal = 0 
    for s in theArray: 
     aTotal += s 
    return aTotal 

def aveValue(theArray): 
    aTotal = totalValue(theArray) 
    aAverage = aTotal/len(theArray) 
    return aAverage 

def dataOut(theArray): 
    print("The maximum value is: " + str(maxValue(theArray))) 
    print("The minimum value is: " + str(minValue(theArray))) 
    print("The total value is: " + str(totalValue(theArray))) 
    print("The average value is: " + str(aveValue(theArray)) + "\n") 

def processInput(theArray): 
    print("The original array was:\n " + str(displayArray(theArray)) + "\n") 
    dataOut(theArray) 
    sortArray(theArray) 
    print("The sorted array is:\n " + str(displayArray(theArray))) 

def unitTest(): 
    print("Running test . . .") 
    randIntArray() 
    theArray = randIntArray() 
    processInput(theArray) 
    print("\n Finished test . . .") 

def main(): 
    unitTest() 
    theArray = [] 
    done = False 
    while not done: 
     proceedQ = input("Would you like to enter 10 numbers <y/n>? ") 
     if proceedQ == "y": 
      getData() 
      processInput(theArray) 
     if proceedQ != "y": 
      done = True 


main() 

再一次感謝大家的參與,你們都是天使。

  • 酒吧
+0

可能是你的意思是存儲由'的getData()返回'入'varibale的theArray'值。所以,你的'theArray'現在是空白的,但是在函數'minValue()'中,你試圖訪問第一個元素。這就是爲什麼錯誤被拋出的原因 – kuro

+0

你的代碼不是Python習慣用法。在Python中,下劃線比CamelCase更受歡迎。在'randIntArray'函數(最好命名爲'generate_random_array')中不需要聲明'randomList = []'。此外,這個函數可以通過調用'random.sample(range(1,101) 10)',不需要額外的函數 - 它增加了絕對沒有價值的代碼 –

+0

'displayList'函數也沒有任何效果,Python中內置'min'和'max'函數,變量如' aTotal','theName'很奇怪,很難打字,在實際生產的python代碼中,你永遠不會找到像這樣的前綴。我建議你首先閱讀Python Tutorial來了解基本知識,用PEP-8來學習標準Python編碼風格。 –

回答

0

main()當你調用getData(),它不會給返回userIntListtheArray

更新main()代碼:

def getData(): 
    userIntList = [] 
    for i in range(10): 
     userNum = getInt("Please enter value number : ") 
     userIntList.append(userNum) 
    return userIntList 

def main(): 
    unitTest() 
    theArray = [] 
    done = False 
    while not done: 
     proceedQ = input("Would you like to enter 10 numbers <y/n>? ") 
     if proceedQ == "y": 
      # Assign the returned list from getdata() to theArray 
      theArray = getData() 
      processInput(theArray) 
     if proceedQ != "y": 
      done = True 
0

main()當你調用函數getData()你沒有捕獲它返回的結果。因此,當您的theArray初始化爲[]時,它將通過processInput()傳遞到minValue(),其值相同,即[]。但在minValue()中,當您嘗試訪問theArray中沒有元素時訪問第一個元素。你需要做的這main() -

theArray = getData() 

而且在python,你並不需要一個單獨的函數來完成交換。它可以這樣做很容易 -

b, a = a, b 

即使你不需要自己排序。嘗試使用Python提供的工具和功能

同去查找列表的最大和最小