Python Forum
computing average in nested loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
computing average in nested loops
#1
I can't seem get class weighted average to compute correctly. I ran program with placement under the two different loops, but each time it gave me an incorrect number.
students = ['Tony', 'Steve']
category = [ 'Assignment', 'Quizzes', 'Projects', 'Essays', 'Exams']
weighted = [.1, .1, .3, .25, .25]
cwave = []
total2 = 0
total3 = 0
for i in students:
    print(i)
    total = 0

    for num in weighted:
        score = float(input('Enter scores: ')) * num
        total += score
        total /= sum(weighted)
        total2 += total
        total2 /= len(students)
        if total >= 89:
            letter = ('A')
        elif total >= 79:
            letter =('B')
        elif total >= 69:
            letter =('C')
        elif total >= 59:
            letter =('D')
        else:
            letter =('F')
    print('Wieghted average = ' + str(total))
    print('Wieghted Letter Grade: ' + (letter))
print('Class Weighted Average: ' + str(total2))
This is what returns - It should compute to 84.225 -no error message:
Error:
/Users/cesarperez/PycharmProjects/Assign7/venv/bin/python /Users/c/Downloads/#4.py Tony Enter scores: 95 Enter scores: 95 Enter scores: 80 Enter scores: 85 Enter scores: 82 Wieghted average = 84.75 Wieghted Letter Grade: B Steve Enter scores: 90 Enter scores: 87 Enter scores: 80 Enter scores: 78 Enter scores: 90 Wieghted average = 83.7 Wieghted Letter Grade: B Class Weighted Average: 65.79052734375 Process finished with exit code 0
Reply
#2
I'll start with one hint: what is the value of sum(weighted)?
Reply
#3
sum(weighted) = 1. The value of
    for num in weighted:
        score = float(input('Enter scores: ')) * num
        total += score
        total /= sum(weighted)
        total2 += total
        total2 /= len(students)
is what I am trying to use. Since sum(weighted) = 1, does it affect the value of total?
Reply
#4
cap510 Wrote:Since sum(weighted) = 1, does it affect the value of total?
Well, if you were adding it to a number, it would affect it (a little bit). But dividing by 1 has no impact, so the line could be omitted. If that were true, then your algorithm would be able to calculate a weighted average without knowing the weights, but that's clearly absurd.

What I was hoping you'd do was realize that your algorithm is wrong, and google weighted average[ :).
Investopedia Wrote:In calculating a weighted average, each number in the data set is multiplied by a predetermined weight before the final calculation is made.

If you're still not sure, I recommend working it out on paper first. Also, I know some people learn well from Youtube (I'm not one of them) so that may be another way to try to learn about weighted averages.
Reply
#5
@micseydel - they multiple by weight on the same line (12 or 2 in the different snippets they post) on which they take the input (num is actually respective weight). But of course you are right - they have weights in range (0-1) and sum of weights is 1, so there is no need to divide by 1. I was thinking to comment on this in the other thread but it slipped from my mind when posting an answer.

EDIT: I see your point now
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Though you get the correct answer for a student's weighed average this is only because the sum of the weights is 1. Try changing your weights to [10, 10, 30, 25, 25]. This should compute the same weighted average as [0.1, 0.1, 0.3, 0.25, 0.25] but it does not when run in your program.

Fixing the above problem should point out the error you are making calculating the classroom average.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with nested loops robert5502001 7 3,602 Aug-01-2022, 06:26 AM
Last Post: stevensanders
  Capitalize first letter of names in nested loops student_of_python 9 4,752 Oct-27-2019, 07:51 AM
Last Post: Larz60+
  nested for loops to recursion ashkea26 4 3,505 Nov-02-2018, 05:00 PM
Last Post: ichabod801
  Computing average vestkok 2 2,518 Aug-12-2018, 10:02 AM
Last Post: vestkok
  Nested loops in openCV JimmyJangle 1 4,831 Apr-17-2018, 04:10 AM
Last Post: Mekire
  Computing factorials Truman 6 4,100 Mar-14-2018, 06:38 AM
Last Post: DeaD_EyE
  Nested for loops with numbers Liquid_Ocelot 7 5,871 Aug-15-2017, 06:38 AM
Last Post: nilamo
  computing the factorial of N foolsgold27 12 8,113 Aug-02-2017, 02:29 AM
Last Post: nilamo
  Nested loops, lists and if statements Liquid_Ocelot 10 8,987 Apr-23-2017, 02:02 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

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