python - How to Add an Index to an Index -
ok. first time asking question please don't complicated me. trying set python program takes list of 3 different categories of scores: quizzes, programs, , tests, , puts them in list. code looks this:
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): eachscore in globalconstantlist: #totalscorelist = 0.0 index = 0 index in range(quiz_grades): print("what score quiz", index + 1) scorelist[index] = float(input()) index in range(program_grades): print("what score program", index + 1) scorelist[index] = float(input()) index in range(tests): print("what score test", index + 1) scorelist[index] = float(input()) return scorelist main()
(sorry if has not been placed in code sample- fyi syntax correct.)
here's issue: every time run code, add quiz_grades
values scorelist[]
list quiz_grades
for-loop, when run program_grades
for-loop, take out original values (from quiz_grades
) , put in program_grades
). example, if enter 99, 98, , 97 quiz scores, , 96 program score, erase initial 99 , place in 96. there way can fill in entire scorelist
without erasing of values?
i think should appending list:
scorelist.append(float(input()))
in every loop, assuming scorelist
empty when function starts. thus, code should become:
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 = [] eachscore in globalconstantlist: #totalscorelist = 0.0 index = 0 in range(quiz_grades): index += 1 print("what score quiz", index) scorelist.append(float(input())) in range(program_grades): index += 1 print("what score program", index) scorelist.append(float(input())) in range(tests): index += 1 print("what score test", index) scorelist.append(float(input())) return scorelist main()
alternatively use start
, stop
parameters of range()
:
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): eachscore in globalconstantlist: #totalscorelist = 0.0 #index = 0 <-- not needed index in range(quiz_grades): print("what score quiz", index + 1) scorelist[index] = float(input()) imin = quiz_grades index in range(imin, program_grades + imin): print("what score program", index + 1) scorelist[index] = float(input()) imin += program_grades index in range(imin, tests + imin): print("what score test", index + 1) scorelist[index] = float(input()) return scorelist main()
Comments
Post a Comment