Python Forum
nested looping with list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
nested looping with list
#1
I'm trying to compute weighted scores using a list. I am looping through a list of students, having user input scores. With scores, I am computing weighted scores and printing the weighted score. When I enter second student's score, the new score continues with scores from previous student. How do I fix score being added to previous student? Ive tried adjusting print statement and using range function.

students = ['Tony','Steve' ]
category = [ 'Assignment', 'Quizzes', 'Projects', 'Essays', 'Exams']
weighted = [.1, .1, .3, .25, .25]

total = 0
for i in students:
    print(i)

    for num in weighted:
        score = float(input('Enter scores: ')) * num
        total += score
        total /= sum(weighted)
    print("wieghted average = " + str(total))
    print()
This is what returns:
Tony
Enter scores: 95
Enter scores: 95
Enter scores: 80
Enter scores: 85
Enter scores: 82
wieghted average = 84.75

Steve
Enter scores: 95
Enter scores: 95
Enter scores: 80
Enter scores: 85
Enter scores: 82
wieghted average = 169.5
Reply
#2
Move the total=0 statement inside the for i in students loop (make it the first line in the loop). That will reset total and keep the information separate.
Reply
#3
Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Input validation for nested dict and sorting list of tuples ranbarr 3 3,910 May-14-2021, 07:14 AM
Last Post: perfringo
  Looping to Create Nested Dictionary gngu2691 10 33,550 Jun-22-2018, 04:11 PM
Last Post: anickone

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020