2017-07-26 129 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()方法