1

What I have coded

students = ['Rose', 'Dorothy', 'Sophia', 'Blanch']

for i in range(len(students)):
    print('Hey',students[i],',please input the folowing grades:')
    weightAvg = []
    discussionGrade = int(input('What was your discussion grade?: ')) # prompt grade 1
    quizGrade = int(input('What was your quiz grade?: ')) # prompt grade 2
    programGrade = int(input('What was your programming assignment grade?: ')) #prompt grade 3
    weightAvg.append (discussionGrade*0.20 + quizGrade*0.30 + programGrade*0.50) #calculate weighted average
    print('Your weighted grade point average is: ',weightAvg, '\n')`

The Output so far

Hey Rose ,please input the folowing grades:
What was your discussion grade?: 78
What was your quiz grade?: 88
What was your programming assignment grade?: 98
Your weighted grade point average is:  [91.0] 

Hey Dorothy ,please input the folowing grades:
What was your discussion grade?: 45
What was your quiz grade?: 99
What was your programming assignment grade?: 87
Your weighted grade point average is:  [82.2] 

Hey Sophia ,please input the folowing grades:
What was your discussion grade?: 67
What was your quiz grade?: 95
What was your programming assignment grade?: 77
Your weighted grade point average is:  [80.4] 

Hey Blanch ,please input the folowing grades:
What was your discussion grade?: 7
What was your quiz grade?: 45
What was your programming assignment grade?: 99
Your weighted grade point average is:  [64.4]

My Question

Essentially I want this output

print("The best student is (student name) with a score of (highest average)")

I have placed my first block in a function and tried to return weightAvg but im still not outputing what I would like.

Any help and explanation would be fantastic! Thank you!

azro
  • 53,056
  • 7
  • 34
  • 70
  • 2
    You didn't actually ask a question. Please read [ask]. One problem with the question as currently formulated is that you say" im still not outputing what I would like." but never bother to inform those reading the question what output you would like. We could maybe guess -- but it would be better if you clearly communicate both the intended output and what you are seeing instead. – John Coleman Nov 27 '22 at 15:22

1 Answers1

2

You need to store the averages outside the loop, to be able to have all of them once ended

Put the pairs (avg, student) in the loop, and at the end use max function, as we put the average value in the tuple, the items will be compared by that, if we had add (student, avg), we would have had the max in term of lexicographical order (the nearest from end of alphabet

weightAvg = []
for student in students:
    print('Hey', student, ',please input the folowing grades:')
    discussionGrade = int(input('What was your discussion grade?: ')) 
    quizGrade = int(input('What was your quiz grade?: '))  
    programGrade = int(input('What was your programming assignment grade?: '))  
    avg = discussionGrade * 0.20 + quizGrade * 0.30 + programGrade * 0.50
    weightAvg.append((avg, student))  # calculate weighted average
    print('Your weighted grade point average is: ', avg, '\n')

best_avg, best_student = max(weightAvg)
print(f"The best student is {best_student} with a score of {best_avg}")
azro
  • 53,056
  • 7
  • 34
  • 70