好的。這是我第一次問一個問題,所以請不要太複雜。我正在嘗試設置我的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
而不會擦除任何我的價值觀?由於
scoreList.append – enderland