Files and Exceptions 3 (VS Code Python) Assignment Objectives you must include the following techniques: Your program MUST USE the main function, which must be called correctly (use the template). Additionally, you must include docstrings as shown in the template. File Input File Output Opening Files Closing Files Using Loops to Process Files Processing Records Exceptions 3. Exception Handling Exceptions offer a gracious way to exit a program when things go wrong. Modify the program you wrote for exercise 2 to handle the exceptions (See 6.4 Exceptions on page 341). Test the program using the following text files: text file named 6-3-lines1.txt containing 22 1 8923 9054 9204 9 71 200254 57 text file named 6-3-lines2.txt containing 22 1 8923 9054 9204 9 71 corn! 57 Your program must: Open file designated by user input for reading Include a try block in the main function that holds most of the code from exercise 2. The try block must handle any: IOError exceptions are raised when an attempt is made to open the file. ValueError exceptions are raised when the items read from the file cannot be converted to type float. Use a loop to read all lines from the file Count the number of lines in the file Keep a running total of all the numbers in the file Display the total, the number of lines, and the average of all numbers. All numeric displays must have two decimal points, and the decimal points should align in the column Close the file For the file 6-3-lines1.txt, the output should look like the following: Enter the name of the file: 6-3-lines1.txt Total: 227,595.00 # of Lines: 9.00 Average: 25,288.33 For the file 6-3-lines2.txt, the output should look like the following: Enter the name of the file: 6-3-lines2.txt Non-numeric data is found in the file. If you try to load a file name that does not exist, the output should look like the following: Enter the name of the file: wrong.txt An error occurred while trying to read the file. Try loading the same files in the code from exercise #2, recreate the same mistakes, and compare how exception handling makes for a more user-friendly experience.
Files and Exceptions 3 (VS Code Python)
Assignment Objectives
you must include the following techniques:
- Your
program MUST USE the main function, which must be called correctly (use the template). Additionally, you must include docstrings as shown in the template. - File Input
- File Output
- Opening Files
- Closing Files
- Using Loops to Process Files
- Processing Records
- Exceptions
3. Exception Handling
Exceptions offer a gracious way to exit a program when things go wrong. Modify the program you wrote for exercise 2 to handle the exceptions (See 6.4 Exceptions on page 341).
Test the program using the following text files:
text file named 6-3-lines1.txt containing
22
1
8923
9054
9204
9
71
200254
57
text file named 6-3-lines2.txt containing
22
1
8923
9054
9204
9
71
corn!
57
Your program must:
- Open file designated by user input for reading
- Include a try block in the main function that holds most of the code from exercise 2.
- The try block must handle any:
- IOError exceptions are raised when an attempt is made to open the file.
- ValueError exceptions are raised when the items read from the file cannot be converted to type float.
- Use a loop to read all lines from the file
- Count the number of lines in the file
- Keep a running total of all the numbers in the file
- Display the total, the number of lines, and the average of all numbers.
- All numeric displays must have two decimal points, and the decimal points should align in the column
- Close the file
For the file 6-3-lines1.txt, the output should look like the following:
Enter the name of the file: 6-3-lines1.txt
Total: 227,595.00
# of Lines: 9.00
Average: 25,288.33
For the file 6-3-lines2.txt, the output should look like the following:
Enter the name of the file: 6-3-lines2.txt
Non-numeric data is found in the file.
If you try to load a file name that does not exist, the output should look like the following:
Enter the name of the file: wrong.txt
An error occurred while trying to read the file.
Try loading the same files in the code from exercise #2, recreate the same mistakes, and compare how exception handling makes for a more user-friendly experience.


Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images









