In: Computer Science
Write a complete and
syntactically correct Python program to solve the following
problem:
Write a program for your professor that allows him to keep a record
of the students’ average grade in his class. The program must be
written in accordance with the following specs:
1. The input must be interactive from the keyboard. You will take
input for 12 students.
2. You will input the students’ name and an average grade. The student cannot enter an average below zero or above 100. Your program must raise and handle an exception should this occur.
a. The exception should cause the program to return and get a new grade
3. Write all output to a file named grades.txt
4. Close the output file.
5. Open the file grades.txt for input.
6. Your program will raise and handle an exception if the file is not found.
a. I will
test this aspect by changing the file name and looking for your
exception
code (your exception should cause program to ask for correct file
name).
7. Read all the records from the file and display them.
Please help me to finish this code:
name,grade=input().split()
grade=int(grade)
if grade <0 or grade>100:
exceptioncheck()
def execptioncheck():
name,grade=input().split()
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#code
#main method def main(): #count of students count=12 #output file name filename='grades.txt' #opening output file in write mode. assuming there is no error check needed here out=open(filename,'w') #looping for count times for i in range(count): #asking for name name=input("Enter student#{} name: ".format((i+1))) done=False #a loop controller variable #looping until done is True while not done: try: #reading grade grade=float(input("Enter average grade: ")) #if grade is invalid, raising an exception if grade <0 or grade>100: raise Exception() #if no exception occurred, setting done to True done=True except: #grade is below 0 or above 100 or non numeric print("Invalid grade, try again") #writing name and grade to output file separated by ' : ' out.write('{} : {}\n'.format(name,grade)) #closing file saving changes out.close() #another loop controller done=False #you can set filename="something else" to see if exceptions are getting handled #properly while not done: try: #trying to open file with filename in read mode inp=open(filename,'r') #if no exceptions occurred, setting done to True done=True except: #exception occurred, asking for different file name print(filename,'not found, enter a valid file name: ') filename=input() #looping through each line in file and displaying it for line in inp: print(line.strip()) #closing file inp.close() #calling main() main()