Question

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.

Homework Answers

Answer #1
# Custom exception class
class OutOfRange(Exception):
    pass


i = 1  # Variable to keep track of how many students are entered
file_write = open('grades.txt', 'w')

# Iterate loop 12 times
while i <= 12:
    # Ask user to enter student name
    name = input('Enter student name ' + str(i) + ': ')
    # In try-except block, ask user to enter average grade
    # If its not in range, raise OutOfRange Exception
    # Catch it in except block and print the error
    while True:
        try:
            average = float(input('Enter average grade: '))
            if average < 0 or average > 100:
                raise OutOfRange
            break
        except OutOfRange:
            print('ERROR: Average must be between 0-100')
    # Increment i by 1
    i += 1
    # Write name and grade in file
    file_write.write('%-15s%-15.2f\n' % (name, average))

# Close the file
file_write.close()

# Take file name and Open file to read
# Do this in a while loop to keep asking user to enter filename till a valid name is entered
while True:
    try:
        filename = input('Enter file name: ')
        file_read = open(filename, 'r')
        break
    except FileNotFoundError as a:
        print(a)

# Print header
print('%-15s%-15s' % ("Name", "Average"))
# Read file till the last line
# Split line and store it in data
# Print name(data[0]) and average(data[1])
for line in file_read.readlines():
    data = line.strip().split()
    print('%-15s%-15.2f' % (data[0], float(data[1])))

# close the file
file_read.close()

SCREENSHOT

OUTPUT

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. Credit Score Annual Intrest Rate 500-649 %15.5 650-729 %9.5 730-800 %4.5 The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range, it asks him/her to enter a numeric...
I need python code for this. Write a program that inputs a text file. The program...
I need python code for this. Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Uppercase words should take precedence over lowercase words. For example, 'Z' comes before 'a'. The input file can contain one or more sentences, or be a multiline list of words. An example input file is shown below: example.txt the quick brown fox jumps over the lazy dog An example of the program's output...
Write a C++ program. 1) Write a program that writes the grades for 3 students. Declare...
Write a C++ program. 1) Write a program that writes the grades for 3 students. Declare a variable of type ofstream which is used to output a stream into a file: ofstream output; // output is the name of the variable Prompt the user to input values for the grades of 3 students. Use the output operator (<<) to write the grades into grades.txt: output << grade1 << " " << grade2 << " " << grade3 << endl; You...
Write a program that reads a file named input.txt and writes a file that contains the...
Write a program that reads a file named input.txt and writes a file that contains the same contents, but is named output.txt. The input file will contain more than one line when I test this. Do not use a path name when opening these files. This means the files should be located in the top level folder of the project. Do not use a copy method that is supplied by Java. Your program must read the file line by line...
The following code to run as the described program on python. The extra test file isn't...
The following code to run as the described program on python. The extra test file isn't included here, assume it is a text file named "TXT" with a series of numbers for this purpose. In this lab you will need to take a test file Your program must include: Write a python program to open the test file provided. You will read in the numbers contained in the file and provide a total for the numbers as well as the...
Write a C program: Just need this beginning part... How to do the file reading stuff....
Write a C program: Just need this beginning part... How to do the file reading stuff. The program will be invoked as P0 [file] where file is an optional argument. If the file argument is not given the program reads data from the keyboard as a device (10%). If the argument is given, the program reads data file file.fs19. (note that file is any name and the extension is implicit). Programs improperly implementing file name or executable will not be...
Python Programming Build a python programming that asks the user for a three-letter substring. The program...
Python Programming Build a python programming that asks the user for a three-letter substring. The program should then proceed to read the file strings.txt. Each line in strings.txt will contain a string. Your program should then test to see how many non-overlapping occurrences of the three-letter substring occur in that string and test whether the string is a palindrome. The program should then proceed to create an output file string_stats.txt, which contains the original data on one line and the...
In Python: Problem 4. Write a program [call it minmax.py] that accepts three integers in the...
In Python: Problem 4. Write a program [call it minmax.py] that accepts three integers in the range [0-100] as input from the user. Your program should then determine and print the smallest and largest integers in the values entered by the user using comparison operators *not* using predefined min or max Python functions. For instance: If the user input is: 36 5 99 your program should give as output: min = 5 max = 99 If the user input is:...
I can open the file in the program, but I cannot figure out how to properly...
I can open the file in the program, but I cannot figure out how to properly split the data and assign a grade to the number values in the text file. I keep getting :: ValueError: invalid literal for int() with base 10: 'Roger Jones 75\n' Below are the assignment details: This program processes grades for a small class. It reads an input file named grade_input.txt where each record contains a student’s first name, last name and numeric grade (a...
3. For this problem, you should describe the algorithm using a flowchart and then write Python...
3. For this problem, you should describe the algorithm using a flowchart and then write Python code to implement the algorithm. You will write the Python code in a Python program file and then run it to obtain the output. Include in your HW document a photo of your flowchart and a screenshot of your code and the output in IDLE you get when you run the program. Algorithm: Ask the user to enter the user’s first name, the number...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT