97.4k views
2 votes
IN python 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 afield for the player’s score.) 2. A program that reads the records from the golf.txt file and displays them.

User Andrine
by
5.7k points

2 Answers

3 votes

Final answer:

To write the player's name and score to a file, open the file in write mode, use a loop to gather input, and write to the file. To read the records from the file, open it in read mode, use a loop to read each line, and display the data.

Step-by-step explanation:

The subject of this question is Computers and Technology.

To complete the first program, you can use the 'open' function in Python to open the 'golf.txt' file in write mode ('w'). Then, you can use a loop to ask the user for each player's name and score, and write them to the file using the 'write' method. Finally, you should close the file using the 'close' method.

To complete the second program, you can again use the 'open' function to open the 'golf.txt' file in read mode ('r'). Then, you can use a loop to read each line of the file and display the player's name and score to the console. Finally, you should close the file using the 'close' method.

User Nate Barr
by
6.2k points
0 votes

Answer:

Check the explanation

Step-by-step explanation:

We can use a while loop to keep getting names end scores from the user. We just need to start with a single name as a priming read. Assuming it, not a 'q' (for quit), the while loop is entered where the score is obtained from the user and both the name and score are written to the file. Then we read another name and check if the user has decided to quit yet.

Note: because this exercise doesn't ask us to modify records of already existing players, we can assume that we should open the file in write mode instead of append mode (or else we'd potentially end up with multiple records for the same golfer). But in a real scenario you'd need to be very explicit with the customer that this program will delete any data that’s already in the file when it is run

#creat and open the file in write mode

File_obj = open(‘golf.text’, ‘w’)

#obtain the first player’s name

Name = input(’enter the name of a golfer or q if you/’re done: ‘)

while name != 'q' and name !=’Q’:

#Obtain the player' e score

score + int (input ('Enter the golf score if player ' + name + ':’ ))

#Write a record if the player's name and score to the file

File_obj.write (name +’/n’)

File_obj.write (str(score) +’/n’)

Obtain the next player’s name or quit

Name=inPut (' /nEnter the name of a golfer or q if you/’re done:’)

#close the file

File_obj.close()

In this part, we’ll want to do a priming read consisting of the name of the first record in the file. Then we’ll get the associated score and print both separate columns. A while loop will continue getting names and scores until we run out of records and file_obj.readline() returns the empty string.

note that we’ll need to strip the extra new line escape sequence character from the names in the records. We don't need to for the scores because the int () function drops that character.

As in my previous solutions, I like to underline the header of my tables. That line should run on your system regardless of your OS, but if not, then just remove the strings ' /033[4m' and '/033[0m' and the header will print without being underlined.

#Open the file in read mode

file_obj = open(’golf.txt', 'r')

#Read name of first record and remove trailing new line escape sequence

name file_obj.readline().rstrip(’/n’)

#Print header far table

print(' \ n'+’ \003[4m’+’name Scare' ’\033[0m’)

#keep retrieving and printing names and scores from the file until the EOF while name !=":

Score=int(file_obj.readline())

print(format(name, '12s’), format(score, '>10d’)) #align +alumna

name = file_obj.readline().rstrip(’\\’)

# Close the file

file_obj.close()

User TheSharpieOne
by
6.2k points