Question
I wrote the following code: def write_students(): outfile = open(students.txt,'w') student_name= input(Enter student name;enter quit to stop: ) while student_name!= 'quit': major = input(Enter major:
I wrote the following code:
def write_students():
outfile = open("students.txt",'w')
student_name= input("Enter student name;enter quit to stop: ")
while student_name!= 'quit':
major = input("Enter major: ")
gpa = input("Enter gpa: ")
outfile.write(student_name + ' ')
outfile.write(major + ' ')
outfile.write(gpa + ' ')
student_name= input("Enter student name;enter quit to stop: ")
outfile.close()
def read_students(major):
infile = open("students.txt",'r')
line= infile.readlines()
maximum = 0
for line in infile:
l = line.strip().split()
if l[1] == major:
maximum = max(maximum,float(l[2]))
infile.close
def main():
write_students()
major = input("Enter major: ")
max_gpa = read_students(major)
print("The highest GPA for" , major , "majors is " ,max_gpa)
main()
I've got the output as follow:
Enter student name;enter quit to stop: john
Enter major: insy
Enter gpa: 3.5
Enter student name;enter quit to stop: willis
Enter major: mana
Enter gpa: 3.5
Enter student name;enter quit to stop: smith
Enter major: insy
Enter gpa: 3.8
Enter student name;enter quit to stop: quit
Enter major: isny
The highest GPA for isny majors isNone
The problem's output as follow:
function write_students that asks the user for student records and writes them to file students.txt.A record of a student includes name, major and GPA.The user can enter as many records as they want until they type in quit.
function read_students that accepts a major.The function returns the highest GPA for that major.
a program that calls write_students and read_students and prints the major and highest GPA.The programwill ask the user for the major for which to search.Assume all names and majors will be lower case.
Sample input/output might look as follows:
Enter student name; enter quit to stop: jones
Enter major: insy
Enter gpa: 2.0
Enter student name; enter quit to stop: smith
Enter major: insy
Enter gpa: 3.25
Enter student name; enter quit to stop: willis
Enter major: mana
Enter gpa: 3.25
Enter student name; enter quit to stop: quit
Enter major: insy
The highest GPA for insy majors is 3.25
I need help regarding the highest GPA . Thank you in advance
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started