I am trying to write a program where i need to be able to enter 5 student marks across 4 different subjects and then output the highest average mark of both the student and the subject.
The desired input and output is :
Student 1 (courses 1-4): 50 60 70 60
Student 2 (courses 1-4): 100 90 87 90
Student 3 (courses 1-4): 70 100 90 90
Student 4 (courses 1-4): 30 65 50 50
Student 5 (courses 1-4): 58 50 74 43
The highest average mark of students: 91.75
The highest average mark of courses: 74.2
The current code I have works for calculating for one subject and not 4. How am I able to enter 4 grades per student to get my desired output.
see my code below:
m1 = int(input("Student 1 (courses 1-4): "))
m2 = int(input("Student 2 (courses 1-4): "))
m3 = int(input("Student 3 (courses 1-4): "))
m4 = int(input("Student 4 (courses 1-4): "))
m5 = int(input("Student 5 (courses 1-4): "))
avg = (m1 m2 m3 m4 m5) / 5;
avg1 =(m1 m2 m3 m4 m5) / 20;
print("The Highest average mark of students =", avg)
print("The Highest average mark of courses =", avg1)
CodePudding user response:
This code does the job,
import pandas as pd
import numpy as np
# Part 1
student_num = 5
all_marks = []
for i in range(student_num):
marks = input(f"Student {i 1} (courses 1-4): ")
all_marks.append(list(map(float, marks.split(" "))))
# Part 2
marks = pd.DataFrame(all_marks)
course_avg = marks.apply(np.mean)
student_avg = marks.apply(np.mean, axis = 1)
The first part of the code converts the input into numbers and stores then int a list. This list is then converted into a dataframe in Part 2. I firstly apply np.mean
on the columns to find the average for each course, then on the rows to find the average for each student.
You can use idxmax()
on both course_avg
and student_avg
to find the index of the maximum average and find the course/student with the highest average accordingly.
(It's better to store the values in a .xlsx or .csv file directly instead of inputing them through Python this way. Once you have the files, just pass the file path in pd.read_excel()
or pd.read_csv()
depending upon the format of the file.)
CodePudding user response:
First, create a dictionary to take the inputs of marks for each student. Lets call it d_marks
. Create a dictionary to get the average of all students. Let's call it avg_marks
. Create a dictionary to get the total marks of all courses. Let's call it avg_course
. After that get the max
of them. Here's how you can do it:
d_marks = {}
avg_marks = {}
avg_course = {}
for i in range(1,6): # We loop for 5 students
d_marks[f'Student {i} (courses 1-4)'] = list(map(int, input(f"Student {i} (courses 1-4): ").split())) # Split the string and creates an integer list
avg_marks[f'Average of Student {i}'] = sum(d_marks[f'Student {i} (courses 1-4)']) / len(d_marks[f'Student {i} (courses 1-4)']) #Create average dictionary for Students
for j in range(1, len(d_marks[f'Student {i} (courses 1-4)']) 1):
if f'Course {j} sum' in avg_course: # if course sum already in dictionary then add it to previous.
avg_course[f'Course {j} sum'] = d_marks[f'Student {i} (courses 1-4)'][j-1]
else:
avg_course[f'Course {j} sum'] = d_marks[f'Student {i} (courses 1-4)'][j-1] # if course sum not in dictionary then create one.
print("The Highest average mark of students =", max(avg_marks.values()))
print("The Highest average mark of courses =", max(avg_course.values())/ len(d_marks))
Output:
Student 1 (courses 1-4): 50 60 70 60
Student 2 (courses 1-4): 100 90 87 90
Student 3 (courses 1-4): 70 100 90 90
Student 4 (courses 1-4): 30 65 50 50
Student 5 (courses 1-4): 58 50 74 43
The Highest average mark of students = 91.75
The Highest average mark of courses = 74.2