I'm trying to calculate the average score. but there seems to be some problem with my code. I'm not sure what to do but it says in the introduction: f) Create a new list called avg_scores that is the list of average extra credit test scores of Marla, Ashford, and Sam. • Use the data from the updated all_scores to calculate the averages. You do NOT need nested for loops for this step. • Use a single for loop to access each inner list, then calculate the average of the inner list. Append that average to the avg_scores list. • Display avg_scores.
# a
m_list = []
print("Please enter Marla's scores one by one.")
for i in range(3):
m_test = int(input("Enter a score: "))
m_list.append(m_test)
print("Marla's scores: ", m_list)
# b
a_list = []
print("Please enter Ashford's scores one by one.")
for i in range(3):
a_test = int(input("Enter a score: "))
a_list.append(a_test)
print("Ashford's scores: ", a_list)
# c
s_list = []
print("Please enter Sam's scores one by one.")
for i in range(3):
s_test = int(input("Enter a score: "))
s_list.append(s_test)
print("Sam's scores: ", s_list)
# d
all_scores = []
for i in (m_list, a_list, s_list):
all_scores.append(i)
print("All scores: ", all_scores)
# e
for i in range(len(all_scores)):
for p in range(len(all_scores[i])):
all_scores[i][p] = all_scores[i][p] 2
print("All scores after extra point:", all_scores)
# f
sum_scores = sum(all_scores)
len_scores = len(all_scores)
avg_scores = sum_scores/len_scores
print("Average scores: ", avg_scores)
CodePudding user response:
all_scores
is a 2-D list, so you need to take each list and add its sum to sum_scores
using a for
loop:
avg_scores = []
for scores in all_scores:
avg_scores.append(sum(scores) / len(scores))
Alternatively, if you want something more concise, you can do:
avg_scores = list(score / 3 for score in map(sum, all_scores))
(I've only included this approach for completeness; most developers would not reasonably expect someone just starting out to be familiar with this syntax.)
CodePudding user response:
To compute the average for each student, you need to take sum(scores) / len(scores)
for each scores
list in all_scores
, i.e.:
avg_scores = [sum(scores) / len(scores) for scores in all_scores]
Note that you can take advantage of for
loops to get the scores for each student without having to copy and paste the same code for each one individually:
all_scores = []
for name in ("Marla", "Ashford", "Sam"):
print(f"Please enter {name}'s scores one by one.")
all_scores.append([int(input("Enter a score: "))for _ in range(3)])
print("All scores:", all_scores)
all_scores = [[i 2 for i in scores] for scores in all_scores]
print("All scores after extra point:", all_scores)
avg_scores = [sum(scores) / len(scores) for scores in all_scores]
print("Average scores:", avg_scores)
Please enter Marla's scores one by one.
Enter a score: 5
Enter a score: 5
Enter a score: 4
Please enter Ashford's scores one by one.
Enter a score: 3
Enter a score: 5
Enter a score: 4
Please enter Sam's scores one by one.
Enter a score: 5
Enter a score: 5
Enter a score: 5
All scores: [[5, 5, 4], [3, 5, 4], [5, 5, 5]]
All scores after extra point: [[7, 7, 6], [5, 7, 6], [7, 7, 7]]
Average scores: [6.666666666666667, 6, 7]