So, my task is to write a program which gets an integer 𝑛 on the input, then 𝑛 times asks for the student’s name (one word) and a space-separated list of their grades, and on the output there should be a list of tuples, where each tuple consists of the student’s name and their grade point average, rounded to integers.
Example:
Input:
3
Ann 5 6 7 8
Ben 10 10 0 0
Fred 7 6 8 5
Output:
[(‘Ann’, 6), (‘Ben’, 5), (‘Fred’, 6)]
Now my program looks like this:
n = int(input())
a = list()
for i in range(n):
a.append(input().split())
print(tuple(a))
I have no idea how to sum numbers in each row (except for the first string, student’s name) and make it like a new list. Maybe I should somehow use numpy in this case, but still don’t know how. I will be grateful for any help
>Solution :
- After splitting each line on a space, the first element of the result is the name, and the rest of the elements are the grades.
- Use
sumto get the total of all grades, then divide by the number of grades to get the average.
n = int(input())
a = []
for i in range(n):
name, *grades = input().split()
a.append((name, round(sum(map(int, grades)) / len(grades))))
print(a)