Ask Question

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.

+2
Answers (1)
  1. 5 February, 00:11
    0
    Check the explanation

    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 ('/n')

    # Close the file

    file_obj. close ()
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “IN python The Springfork Amateur Golf Club has a tournament every weekend. The club president has asked you to write two programs: 1. A ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers