42.7k views
0 votes
write a python program that uses a file named scores.txt to save a user's sets of bowling scores for different dates.

1 Answer

4 votes

# Open scores.txt file in append mode

file = open("scores.txt", "a")

# Loop to enter scores for multiple dates

while True:

date = input("Enter the date (MM/DD/YYYY) of your bowling scores (press q to quit): ")

# Exit loop if user enters q

if date == 'q':

break

# Ask user for scores and write to file

scores = input("Enter your bowling scores for this date (separated by spaces): ")

file.write(date + "," + scores + "\\")

# Close the file

file.close()

This program prompts the user to enter a date and their bowling scores for that date, which are then saved in the "scores.txt" file separated by commas. The program will keep looping and asking the user for new dates and scores until they enter "q" to quit.

To read the saved scores from the file, you could use the following code:

# Open scores.txt file in read mode

file = open("scores.txt", "r")

# Loop through lines in file

for line in file:

# Split line into date and scores

date, scores = line.strip().split(",")

# Convert scores to list of integers

scores = [int(score) for score in scores.split()]

# Do something with the date and scores (e.g. print them)

print(date, scores)

# Close the file

file.close()

This code reads each line in the file, splits it into the date and scores, converts the scores to a list of integers, and then does something with the date and scores (e.g. print them).

User Khanghoang
by
8.1k points