56.4k views
5 votes
Create the following functions: calculateAverage(studentName): returns the average of a student's grades according to the name supplied. lowestGrade(studentName): returns the lowest grade for a student according to the name supplied. highestGrade(studentName): returns the highest grade for a student according to the name supplied. Then, in a main() function, write code that accepts a name from the user, and displays the following text, with the corresponding data:

User Dan Hulton
by
5.5k points

1 Answer

2 votes

Answer:

#a global variable that would store the grades of all students

#it would be a dictionary of lists. One can opt for any other data type

grades={"bob":[3,4,2.5], "alice": [2.3,3.1,2]}

#this is the function that calculates average

def calculateAverage(studentName):

#the for loop is for iteration. we iterate over dictionary items

for student, grade in grades.items():

#we check if student record exists

if student==studentName:

average= sum(grade)/len(grade)

return(average)

else:

print("student does not exist :")

#the calculate Average function ends here

#this is the function that calculates lowest grade

def lowestGrade(studentName):

for student, grade in grades.items():

if student==studentName:

return(min(grade))

else:

print("student does not exist :")

#the lowestgrade function ends here

#this is the function that calculates highest grade

def highestGrade(studentName):

for student, grade in grades.items():

if student==studentName:

return(max(grade))

else:

print("student does not exist :")

#the highest grade function ends here

#main function

def main():

studentname=input("please input student name: ")

print("the average gpa of {}: is {}".format(studentname,calculateAverage(studentname)))

print("the lowest grade of {}: is {}".format(studentname,lowestGrade(studentname)))

print("the heighest gpa of {}: is {}".format(studentname,highestGrade(studentname)))

#main function ends

main()

Step-by-step explanation:

  • we have used python programming to solve this problem
  • in order to create a function, the "def" keyword
  • in each function, we first check whether student record exists or not
  • we iterate over the dictionary, if record is found, we do our calculations and return the value
  • if record is not found, we print a message that student does not exist
  • the input is taken from user in main function through input function, which a builtin python function

User Dorothee
by
5.5k points