2017-07-26 111 views
0

好的。這是我第一次問一個問題,所以請不要太複雜。我正在嘗試設置我的Python程序,以便它包含三個不同類別的分數列表:測驗,程序和測試,並將它們放入列表中。我的代碼如下所示:如何向索引添加索引

QUIZ_GRADES = int(input("How many quiz grades? ")) 

PROGRAM_GRADES = int(input("How many program grades? ")) 

TESTS = int(input("How many tests? ")) 

def main(): 

globalConstantList = [QUIZ_GRADES, PROGRAM_GRADES, TESTS] 

scoreList = [0] * (QUIZ_GRADES + PROGRAM_GRADES + TESTS) 

returnedScoresList = getGrades(globalConstantList,scoreList) 

#totalList(totalScore[scores]) 

#userOutput() 

print(returnedScoresList) 


def getGrades(globalConstantList,scoreList): 

    for eachScore in globalConstantList: 

    #totalScoreList = 0.0 

    index = 0 

     for index in range(QUIZ_GRADES): 

      print("What is the score for quiz", index + 1) 

      scoreList[index] = float(input()) 

     for index in range(PROGRAM_GRADES): 

      print("What is the score for program", index + 1) 

      scoreList[index] = float(input()) 

     for index in range(TESTS): 

      print("What is the score for test", index + 1) 

      scoreList[index] = float(input()) 

     return scoreList 


main() 

(很抱歉,如果一切都沒有被放置在代碼樣品 - FYI語法是正確的。)

這裏是我的問題:每次我跑我的代碼,它會將我所有的QUIZ_GRADES值添加到從QUIZ_GRADES for循環的scoreList[]列表中,但隨後當我運行PROGRAM_GRADES for-loop時,它將取出我的原始值(從QUIZ_GRADES)並放入PROGRAM_GRADES)。例如,如果我爲我的測驗分數輸入99,98和97,爲我的程序分數輸入96,它將刪除最初的99並放置在96中。是否有一種方法可以填寫整個scoreList而不會擦除任何我的價值觀?由於

+0

scoreList.append – enderland

回答

0

我想你應該追加到列表:

scoreList.append(float(input())) 
在每個循環

,假設scoreList是空的功能啓動時。因此,你的代碼應該變成:

def main(): 
    globalConstantList = [QUIZ_GRADES, PROGRAM_GRADES, TESTS] 
    # scoreList = [0] * (QUIZ_GRADES + PROGRAM_GRADES + TESTS) 
    returnedScoresList = getGrades(globalConstantList) 
    #totalList(totalScore[scores]) 
    #userOutput() 
    print(returnedScoresList) 

def getGrades(globalConstantList): 
    scoreList = [] 
    for eachScore in globalConstantList: 
     #totalScoreList = 0.0 
     index = 0 
     for i in range(QUIZ_GRADES): 
      index += 1 
      print("What is the score for quiz", index) 
      scoreList.append(float(input())) 
     for i in range(PROGRAM_GRADES): 
      index += 1 
      print("What is the score for program", index) 
      scoreList.append(float(input())) 
     for i in range(TESTS): 
      index += 1 
      print("What is the score for test", index) 
      scoreList.append(float(input())) 
    return scoreList 

main() 

或者使用range()startstop參數:

def main(): 
    globalConstantList = [QUIZ_GRADES, PROGRAM_GRADES, TESTS] 
    scoreList = [0] * (QUIZ_GRADES + PROGRAM_GRADES + TESTS) 
    returnedScoresList = getGrades(globalConstantList, scoreList) 
    #totalList(totalScore[scores]) 
    #userOutput() 
    print(returnedScoresList) 

def getGrades(globalConstantList, scoreList): 
    for eachScore in globalConstantList: 
     #totalScoreList = 0.0 
     #index = 0 <-- not needed 
     for index in range(QUIZ_GRADES): 
      print("What is the score for quiz", index + 1) 
      scoreList[index] = float(input()) 
     imin = QUIZ_GRADES 
     for index in range(imin, PROGRAM_GRADES + imin): 
      print("What is the score for program", index + 1) 
      scoreList[index] = float(input()) 
     imin += PROGRAM_GRADES 
     for index in range(imin, TESTS + imin): 
      print("What is the score for test", index + 1) 
      scoreList[index] = float(input()) 
    return scoreList 

main() 
0

你可能想寫

for index in range(QUIZ_GRADES): 
    index+=1 
    print("What is the score for quiz", index) 
    scoreList[index] = float(input()) 

和類似的其他for循環。該print(..,index + 1)不會增加索引,並且在每個循環索引中從0開始。因此,在每個for循環的開始處,scoreList [index]評估爲scoreList [0],並且它會覆蓋您插入的內容在之前的循環中。

注意有更優雅的方法來編碼。還檢查append()方法