83.3k views
1 vote
You're helping Professor Joy to calculate the grades for his course. The way his course is organised, students take two exams and their final grade is the weighted average of the two scores, where their lowest score weights 70% and their highest score 30%. To accomplish this, you are going to need to do the following: (1) Build the Student named tuple with the following attributes: name (string) exam1 (float) exam2 (float) (2) Write a function create_student, which will ask the user to input a single student's name, exam 1 score, and exam 2 score. The function will then create a named tuple with this information and return it. (3) Write a function create_class, which an integer n as a parameter, and calls create_student n times and returns a list with the n students created. (4) Write a function calculate_score, which takes a single Student named tuple as a parameter, and returns the final score, where their lowest grade is weighted 70% and their highest grade is weighted 30%.

User Fandro
by
5.2k points

1 Answer

6 votes

Answer:

see explaination

Step-by-step explanation:

# importing "collections" for namedtuple()

from collections import namedtuple

# function creates student records and return the named tuple

def create_student():

Student = namedtuple('Student',['name','exam1','exam2']) # creating the object Student

# taking input the name and scores of the students

name = input()

exam1 = float(input())

exam2 = float(input())

return Student(name, exam1, exam2) # return the named tuple

# function creates list of student records and return it

def create_class(n):

student_list = []

# creating student data for n students

for i in range(n):

student_list.append(create_student()) # storing data in the student_list

return student_list # return the student data i.e. student_list

# function calculates the final score of each student and returns it

def calculate_score(S):

final_score = 0

if S[1] > S[2]: # if exam1 score is greater than exam2

final_score = S[1] * 0.3 + S[2] * 0.7 # final score is sum of 30% of exam1 and 70% exam2

elif S[1] < S[2]: # if exam2 score is greater than exam1

final_score = S[1] * 0.7 + S[2] * 0.3 # final score is sum of 30% of exam2 and 70% exam1

elif S[1] == S[2]: # if both scores are equal

final_score = S[1] # final score is sum if 30% of exam1 and 70% exam2, but here both scores are equal so final score is equal to any one of the score

else: # if scores are invalid

print('Invalid entries')

return final_score # return final score

# driver function

def main():

n = int(input()) # enter the no. of students in class

if n < 1: # if n is less than 1 return nothing and stop execution

return

student_list = create_class(n) # call create_class to create class of n students

for i in student_list:

print(round(calculate_score(i), 2)) # calculate the final score for each student and print it

if __name__ == "__main__": main()

User Blivet
by
5.7k points