Question

In: Computer Science

Write a complete and syntactically correct Python program to solve the following problem: Write a program...

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()

Solutions

Expert Solution

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()

Related Solutions

Write a complete and syntactically correct Python program to solve the following problem: You are the...
Write a complete and syntactically correct Python program to solve the following problem: You are the payroll manager for SoftwarePirates Inc. You have been charged with writing a package that calculates the monthly paycheck for the salespeople. Salespeople at SoftwarePirates get paid a base salary of $2000 per month. Beyond the base salary, each salesperson earns commission on the following scale: Sales Commission Rate Bonus <$10000 0% 0 $10000 – $100,000 2% 0 $100,001 - $500,000 15% $1000 $500,001 -...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that calculates the amount of money a person would earn over a period of time if his or her salary is 1 penny for the first day, 2 pennies for the second day, 4 pennies for the third day, and continues to double each day. 1.      The program should ask the user for the number of days the employee worked. 2.      Display a table showing the salary...
Write a complete Java Program to solve the following problem. February 18 is a special date...
Write a complete Java Program to solve the following problem. February 18 is a special date as this is the date that can be divisible by both 9 and 18 Write a program that asks the user for a numerical month and numerical day of the month and then determines whether that date occurs before, after, or on February 18. If the date occurs before February 18, output the word Before. If the date occurs after February 18, output the...
IN PYTHON Write a program to do the following: Solve the a set of equations as...
IN PYTHON Write a program to do the following: Solve the a set of equations as mentioned in "Zybook 5.20 LAB: Brute force equation solver". Instead of the arithmetic operators use your own function defined in a module named calc. You must provide two files (calc.py, brute_force_solver.py) :- 1) calc.py:- Add function named 'add' instead of using operator '+' [10pts] Add function named 'difference' instead of using operator '-' [10pts] Add function named 'product' instead of using operator '*' [10pts]...
Write a complete Java program to solve the following problem. Read two positive integers from the...
Write a complete Java program to solve the following problem. Read two positive integers from the user and print all the multiple of five in between them. You can assume the second number is bigger than the first. For example if the first number is 1 and the second number is 10, then your program should output 5 10 Java must be grade 11 work easy to understand and not complicated code
Write a Python program to solve the 8-puzzle problem (and its natural generalizations) using the A*...
Write a Python program to solve the 8-puzzle problem (and its natural generalizations) using the A* search algorithm. The problem. The 8-puzzle problem is a puzzle invented and popularized by Noyes Palmer Chapman in the 1870s. It is played on a 3-by-3 grid with 8 square blocks labeled 1 through 8 and a blank square. Your goal is to rearrange the blocks so that they are in order, using as few moves as possible. You are permitted to slide blocks...
A: Write a divide-and-conquer program to solve the following problem:
in Java A: Write a divide-and-conquer program to solve the following problem:     1. Let A[1..n] and B[1..n] be two arrays of distinct integers, each sorted in an increasing order.      2. Find the nth smallest of the 2n combined elements. Your program must run in O(log n) time. For example: n = 4If A[1..n] = {2, 5, 8, 9} and B[1..n] = {1, 4, 6, 7}The nth (i.e. 4th) smallest integer is 5.If A[1..n] = {2, 5, 8, 13}...
Complete the following in syntactically correct Python code. 1. The program should display a message indicating...
Complete the following in syntactically correct Python code. 1. The program should display a message indicating whether the person is an infant, a child, a teenager, or an adult. Following are the guidelines: a. If the person is older than 1 year old or less, he or she is an infant. b. If the person is older than 1 year, but younger than 13, he or she is a child c. If the person is at least 13 years old,...
PYTHON Program Problem Statement: Write a Python program that processes information related to a rectangle and...
PYTHON Program Problem Statement: Write a Python program that processes information related to a rectangle and prints/displays the computed values. The program will behave as in the following example. Note that in the two lines, Enter length and Enter width, the program does not display 10.0 or 8.0. They are values typed in by the user and read in by the program. The first two lines are text displayed by the program informing the user what the program does. This...
please solve the following in python: write a program for the decryption of 2-rail fence cipher....
please solve the following in python: write a program for the decryption of 2-rail fence cipher. For example, the input "Cmhmtmrooeoeoorw" should return "Comehometomorrow", and input "topaesw lyr" should return "two players".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT