In: Computer Science
Python Language:
The Marietta Country Club has asked you to write a program to gather, then display the results of the golf tournament played at the end of March. The Club president Mr. Martin has asked you to write two programs. The first program will input each player's first name, last name, handicap and golf score and then save these records in a file named golf.txt (each record will have a field for the first name, last name, handicap and golf score). The second program will read the records from the golf.txt file and display them with appropriate headings above the data being displayed. There are 16 players in the tournament. Par for the course is 80. The data is as follows: Andrew Marks 11.2 72 Betty Franks 12.8 89 Connie William 14.6 92 Donny Ventura 9.9 78 Ernie Turner 10.1 81 Fred Smythe 8.1 75 Greg Tucker 7.2 72 Henry Zebulon 8.3 83 Ian Fleming 4.2 72 Jan Holden 7.7 84 Kit Possum 7.9 79 Landy Bern 10.3 93 Mona Docker 11.3 98 Kevin Niles 7.1 80 Pam Stiles 10.9 87 Russ Hunt 5.6 73 If the score is = Par, then display 'Made Par' If the score is < Par, then display 'Under Par' If the score is > Par, then display 'Over Par'.
REMEMBER to put your name on the lab with comments, and comment throughout the program with what the program is doing.
CODE:
#program 1
#writing to file golf.txt
scores = []
details =
{'firstName':'','lastName':'','handicap':0,'score':0}
for i in range(16):
#asking the user to enter the details
print('Enter the details of player: ')
details['firstName'] = input('Enter the first name: ')
details['lastName'] = input('Enter the last name: ')
details['handicap'] = float(input('Enter the handicap: '))
details['score'] = float(input('Enter the score: '))
scores.append(details)
details =
{'firstName':'','lastName':'','handicap':0,'score':0}
#opening the file
file = open('golf.txt','w')
for i in scores:
#writing each line
file.write('{} {} {}
{}\n'.format(i['firstName'],i['lastName'],str(i['handicap']),str(i['score'])))
file.close()
print('Files Written!')
#second program
file_in = open('golf.txt','r')
#reads the line from the file
lines = [x.split('\n')[0] for x in file_in.readlines()]
for i in lines:
par = ''
#checking the score of each entry
score = float(i.split(' ')[3])
if(score == 80):
par = 'Made Par'
elif(score < 80):
par = 'Under Par'
elif(score > 80):
par = 'Over Par'
#priting with the heading
print('{}\t{}'.format(i,par))
file_in.close()
____________________________________________________
CODE IMAGES AND OUTPUT:
________________________________________________
golf.txt
Andrew Marks 11.2 72.0
Betty Franks 12.8 89.0
Connie William 14.6 92.0
Donny Ventura 9.9 78.0
Ernie Turner 10.1 81.0
Fred Smythe 8.1 75.0
Greg Tucker 7.2 72.0
Henry Zebulon 8.3 83.0
Ian Fleming 4.2 72.0
Jan Holden 7.7 84.0
Kit Possum 7.9 79.0
Landy Bern 10.3 93.0
Mona Docker 11.3 98.0
Kevin Niles 7.1 80.0
Pam Stiles 10.9 87.0
Russ Hunt 5.6 73.0
_____________________________________________
Feel free to ask any questions in the comments section
Thank You!