Objective
Develop a Python program to prompt the user for marks for 20 students (2 groups of 10, using a two-dimensional array), and display the average for each group.
Example Python Exercise
Show Python Code
# Program developed by: Programmer 1, Programmer 2
# Initialize a 2D list to store marks for 2 groups of 10 students each
marks = []
# Loop through each group (2 groups)
for group in range(2):
group_marks = [] # Temporary list to store marks for the current group
print(f"Enter marks for Group {group + 1} (10 students):")
# Loop through each student in the group
for student in range(10):
while True:
try:
mark = float(input(f"Enter mark for student {student + 1}: "))
if mark < 0 or mark > 100:
print("Please enter a valid mark between 0 and 100.")
else:
group_marks.append(mark) # Add the mark to the group's list
break
except ValueError:
print("Please enter a valid number for the mark.") # Handle invalid input
marks.append(group_marks) # Add the group's marks to the main list
# Calculate and display the average for each group
for group_index in range(2):
group_average = sum(marks[group_index]) / len(marks[group_index])
print(f"The average mark for Group {group_index + 1} is: {group_average:.2f}")
Output
Enter marks for Group 1 (10 students):
Enter mark for student 1: 85
Enter mark for student 2: 90
Enter mark for student 3: 78
Enter mark for student 4: 88
Enter mark for student 5: 92
Enter mark for student 6: 79
Enter mark for student 7: 84
Enter mark for student 8: 77
Enter mark for student 9: 90
Enter mark for student 10: 89
Enter marks for Group 2 (10 students):
Enter mark for student 1: 68
Enter mark for student 2: 75
Enter mark for student 3: 80
Enter mark for student 4: 82
Enter mark for student 5: 70
Enter mark for student 6: 76
Enter mark for student 7: 79
Enter mark for student 8: 72
Enter mark for student 9: 84
Enter mark for student 10: 67
The average mark for Group 1 is: 84.20
The average mark for Group 2 is: 75.30
Share this Python Exercise