Python file IO and exception handling

1. The Calloway Amateur Golf Club has a tournament every weekend. The club president has asked you to write two programs:

a) A function record_score() that will read each player's name and golf score as keyboard input, and save the file as golf_player_score.txt. Each entry record must have fields - Name of Player and Score of the player (Column Headings). Please create a record for at least 15 players with hypothetical names and scores.

b) Write another function read_score() to read contents from golf_player_score.txt and display them with name, score, name, score... and average score from the tournament.

 

2. Write a function five_numbers() that prompts the user for five numbers. Check whether all numbers are greater than 12, if any number is less, ask for another input greater than 12. Now multiply the 5th number by the 2nd number and calculate the sum of the three remaining numbers. Then, compare the sum and product, if the product is greater than the sum then divide the product by the sum otherwise divide the sum by the product. While writing this program, you are required to check for these two errors (Exceptional Handling) - ValueError and ZeroDivisionError.

Hint: Handle multiple errors (try-except suite)

 

3. Write a function random_numbers() file numbers.txt with 35 random numbers from 100 to 200 (use random module). You need to write the Serial Number in the left column and the random numbers in the right column (Serial Number Random Number). After that calculate the sum and average of those random numbers.

 

Now manually modify some numbers by inserting characters into the file so that you will be able to handle the following errors while reading the file:

i) It should handle any IOError exceptions that are raised when the file is opened and data is read from it.

ii) It should handle any ValueError exceptions that are raised when the items that are read from the file are converted to a number.

Hint: Handle multiple errors (try-except suite)

Need a custom answer at your budget?

This assignment has been answered 2 times in private sessions.

© 2024 Codify Tutor. All rights reserved