I have this simple code
students = ['jill', 'bill', 'Gill', 'Fill', 'Dill']
grades = int(input("Enter grade: "))
for student in students:
print(student, grades)
The output is
Enter grade90
jill 90
bill 90
Gill 90
Fill 90
Dill 90
I want to connect each grade to the name and average them up.
CodePudding user response:
Ask for the grade in the loop. To calculate the average, total all the grades and divide by the number of students.
students = ['jill', 'bill', 'Gill', 'Fill', 'Dill']
total = 0
for student in students:
grade = int(input(f"Enter grade for {student}: "))
total = grade
average = round(total/len(students))
print(f"Average grade is {average}")
CodePudding user response:
You need another list to remember the grades:
students = ['jill', 'bill', 'Gill', 'Fill', 'Dill']
grades = [int(input(f"Enter grade for {student}: ")) for student in students]
for student, grade in zip(students, grades):
print(student, grade)
print(f"Average: {sum(grades)/len(grades)}")
Or even better, a dict:
students = ['jill', 'bill', 'Gill', 'Fill', 'Dill']
grades = {student: int(input(f"Enter grade for {student}: ")) for student in students}
for student, grade in grades.items():
print(student, grade)
print(f"Average: {sum(grades.values())/len(grades)}")
Output for both:
Enter grade for jill: 61
Enter grade for bill: 34
Enter grade for Gill: 87
Enter grade for Fill: 94
Enter grade for Dill: 100
jill 61
bill 34
Gill 87
Fill 94
Dill 100
Average: 75.2