KnowledgeBoat Logo

Computer Science

Write a program that interactively creates a nested tuple to store the marks in three subjects for five students and also add a function that computes total marks and average marks obtained by each student.
Tuple will look somewhat like :
marks( (45, 45, 40), (35, 40, 38),(36, 30, 38), (25, 27, 20), (10, 15, 20) )

Python

Python Tuples

3 Likes

Answer

num_of_students = 5
tup = ()

def totalAndAvgMarks(x):
    total_marks = sum(x)
    avg_marks = total_marks / len(x)
    return (total_marks, avg_marks)

for i in range(num_of_students):
    print("Enter the marks of student", i + 1)
    m1 = int(input("Enter marks in first subject: "))
    m2 = int(input("Enter marks in second subject: "))
    m3 = int(input("Enter marks in third subject: "))
    tup = tup + ((m1, m2, m3),)  
    print()  

print("Nested tuple of student data is:", tup)

for i in range(num_of_students):      
    print("The total marks of student", i + 1,"=", totalAndAvgMarks(tup[i])[0])
    print("The average marks of student", i + 1,"=", totalAndAvgMarks(tup[i])[1])
    print()

Output

Enter the marks of student 1
Enter marks in first subject: 25
Enter marks in second subject: 45
Enter marks in third subject: 45

Enter the marks of student 2
Enter marks in first subject: 90
Enter marks in second subject: 89
Enter marks in third subject: 95

Enter the marks of student 3
Enter marks in first subject: 68
Enter marks in second subject: 70
Enter marks in third subject: 56

Enter the marks of student 4
Enter marks in first subject: 23
Enter marks in second subject: 56
Enter marks in third subject: 45

Enter the marks of student 5
Enter marks in first subject: 100
Enter marks in second subject: 98
Enter marks in third subject: 99

Nested tuple of student data is: ((25, 45, 45), (90, 89, 95), (68, 70, 56), (23, 56, 45), (100, 98, 99))
The total marks of student 1 = 115
The average marks of student 1 = 38.333333333333336

The total marks of student 2 = 274
The average marks of student 2 = 91.33333333333333

The total marks of student 3 = 194
The average marks of student 3 = 64.66666666666667

The total marks of student 4 = 124
The average marks of student 4 = 41.333333333333336

The total marks of student 5 = 297
The average marks of student 5 = 99.0

Answered By

1 Like


Related Questions