Anonymous88
Anonymous88

Reputation: 1

How to use 2D array in python that can support different size of array?

Using python program to find the average marks of all subject for all the following students:

Student ID  | Quiz   | Research Project | Assignment     | Final Exam                   
1           | 40/50  | 50/100           | 15/20          | 50/100
2           | 50/50  | 30/100           | 10/20          | 10/100
3           | 22/50  | 43/100           | 8/20           | 30/100
4           | 40/50  | 36/100           | 19/20          | 43/100
5           | 20/50  | 80/100           | 11/20          | 51/100

Rules to follow:

  1. Use 2-Dimensional Array to store the above data (must be float) and perform calculation. Make sure the average is printed in 2 decimal point. (exclude student ID and header in array)
  2. Store your 2D array with variable markss
  3. Make sure your algorithm able to support different size of array.

The sample output should look exactly like that:

student 1 have 63.75 average.
student 2 have 47.50 average.
student 3 have 39.25 average.
student 4 have 63.50 average.
student 5 have 56.50 average.

I have tried to code it, below is my code:

markss = [[0.8, 0.5, 0.75, 0.5], [1, 0.3, 0.5, 0.1], [0.44, 0.43, 0.4, 
0.3], [0.8, 0.8, 0.95, 0.43], [0.4, 0.8, 0.55, 0.51]]


average = [(sum(avg)/float(len(avg)))*100 for avg in markss]

print(f"student 1 have {average[0]:.2f} average.")
print(f"student 2 have {average[1]:.2f} average.")
print(f"student 3 have {average[2]:.2f} average.")
print(f"student 4 have {average[3]:.2f} average.")
print(f"student 5 have {average[4]:.2f} average.")

Upvotes: 0

Views: 71

Answers (1)

ASFAW AYALKIBET
ASFAW AYALKIBET

Reputation: 54

my2dlist = []           # declare normal list

my2dlist.append([x for x in range(2)])   # adding lists
my2dlist.append([x for x in range(9)])   

print(my2dlist)

print(my2dlist[0][1])   # accessing entry

Upvotes: 1

Related Questions