Programming Exercises

1. File Display

Assume that a file containing a series of integers is named numbers . txt and exists on the computer's disk. Write a program that displays all of tlie numbers in the file.

2. File Head Display

Write a program that asks rhe user for rhe name of a file. The program should display only the first five lines of the file's contents. If the file contains less than five lines, it should display the file's entire contents.

3. Line Numbers

Write a program that asks the user for the name of a file. The program should display the contents of the file with each line preceded with a line number followed by a colon. The line numbering should start at 1.

4. Item Counter

Assume that a file containing a series of names (as srrings) is named names . txt and exists on rhe computer's disk. Write a program that displays rhe number of names that are stored in the file. (Hint: Open the file and read every string stored in it. Use a variable to keep a count of the number of items thai are read from the file.)

5. Sum of Numbers

Assume that a file containing a series of integers is named numbers . txt and exists on rhe computer's disk. Write a program that reads all of the numbers stored in the file and calculates their total.

6. Average of Numbers

Assume that a file containing a series of integers is named numbers. txt and exists on rhe computer's disk. Wrire a program that calculates the average of all the numbers stored in the file.

7. Exception Handing

Modify the program that you wrote for Exercise 6 so it handles the following exceptions:

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

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

8. Golf Scores

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

1. A program that will read each player's name and golf score as keyboard input, and then save these as records in a file named golf .txt. (Each record will have a field for the player's name and a field for the player's score.)

2. A program that reads the records from rhe golf .txt file and displays them.

+1 -1

Responses

  • miska
    How to make a golf score program in python?
    2 years ago

Post a comment