SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Files And Exceptions
Exercise:
Programming Exercises
Question:10 | ISBN:9780132576376 | Edition: 2

Question

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 the golf.txt file and displays them.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Golf Scores:

1) First Program code:

def savePlayerRecords():
    try:
        #Prompt and read the each player’s name 
        #and golf score from the user
        numPlayers = int(input("Enter the number of players: "))
        #save these as records in a file named golf.txt
        with open("golf.txt", "w") as file:
            for _ in range(numPlayers):
                playerName = input("Enter player name: ")
                score = input("Enter player score: ")
                file.write(playerName + "," + score + "\n")
        print("Player records saved successfully in file 'golf.txt'.")
    except Exception as e:
        print("An error occurred:", str(e))
savePlayerRecords()

Executed Output:

Enter the number of players: 2
Enter player name: Josh
Enter player score: 80
Enter player name: Don
Enter player score: 90
Player records saved successfully in file 'golf.txt'.

Output data in  'golf.txt' file:

Josh,80
Don,90

2) Second Program code:

def displayPlayerRecords():
    try:
        #Reads the records from the golf.txt file
        #and displays them.
        with open("golf.txt", "r") as file:
            for line in file:
                playerData = line.strip().split(",")
                playerName = playerData[0]
                score = playerData[1]
                print("Player Name:", playerName)
                print("Score:", score)
                print("-----------------------")
    except FileNotFoundError:
        print("Player records file not found.")
    except Exception as e:
        print("An error occurred:", str(e))

displayPlayerRecords()

Data in 'golf.txt' file:

Josh,80
Don,90

Executed Output:

Player Name: Josh
Score: 80
------------------
Player Name: Don
Score: 90
------------------

 

0 0

Discussions

Post the discussion to improve the above solution.