Programming Exercise 3-1
Programming Exercise 6-10
Part I:
def main():
# Local variables
name = ”
golf_score = 0
num_players = 0
# Prompt user for the number of players
num_players = int(input(‘Enter the number of ‘ \
‘players in the tournament: ‘))
# Open golf.txt for writing
outfile = open(‘golf.txt’, ‘w’)
# Write data to file
for i in range(num_players):
# Prompt for name and score
name = input(‘Enter the name of the player: ‘)
golf_score = int(input(‘Enter the golf score: ‘))
# Write data to file
outfile.write(name + ‘\n’)
outfile.write(str(golf_score) + ‘\n’)
# Close file
outfile.close()
# Call the main function.
main()
Part II:
def main():
# Local variables
line = ”
name = ”
golf_score = 0
num_players = 0
# Open golf.txt for reading
infile = open(‘golf.txt’, ‘r’)
# Read first name
name = infile.readline()
# Read until no data
while name != ”:
# Read score
golf_score = int(infile.readline())
# Strip ‘\n’
name = name.rstrip(‘\n’)
# Display data with one line space between the data
# for every two players
print (‘Name:’, name)
print (‘Golf Score:’, golf_score)
# Read next name
name = infile.readline()
# Close file
infile.close()
# Call the main function.
main()