A simple Python program to manage and display user scores. This utility allows you to input a name and a score, saves them to a scores.txt file, and then displays all recorded scores.
- Add New Scores: Easily add a user's name and their corresponding score.
- Data Persistence: Scores are saved to a
scores.txtfile, ensuring your data isn't lost when the program closes. - Score Validation: Ensures that only whole numbers can be entered as scores.
- Display All Scores: After adding a new score, the program displays all entries currently in
scores.txt. - Error Handling: Gracefully handles file-related errors and invalid score inputs.
These instructions will get you a copy of the project up and running on your local machine.
You only need Python installed on your system. This program was developed with Python 3.
-
Clone the repository (if applicable, otherwise skip this step and just create the file):
git clone [https://github.com/your-username/score-manager.git](https://github.com/your-username/score-manager.git) cd score-managerIf you don't have a repository, simply create a file named
score_manager.py. -
Save the code: Create a file named
score_manager.pyand paste the provided Python code into it:def manage_scores(): print("--- Welcome to the Score Manager ---") # Step 1: Prompt the user for their name and score user_name = input("Enter your name: ") # Convert score input to an integer, ensuring it's a number while True: try: user_score = int(input("Enter your score: ")) break # Exit loop if input is a valid integer except ValueError: print("Invalid score. Please enter a whole number.") # Step 2: Open "scores.txt" in append mode ("a") and write the name and score try: with open("scores.txt", "a") as file: # Format the name and score as a string, followed by a newline file.write(f"{user_name}: {user_score}\n") print(f"\nScore for {user_name} saved successfully!") except IOError as e: print(f"Error saving score to file: {e}") return # Exit if there's a file writing error # Step 3: After saving, read the file’s contents and display them print("\n--- All Stored Scores ---") try: with open("scores.txt", "r") as file: all_scores_content = file.read() # Read the entire content of the file if all_scores_content.strip(): # Check if content is not just empty or whitespace print(all_scores_content) # Print the content to the console else: print("No scores found in the file yet.") except FileNotFoundError: print("The 'scores.txt' file does not exist yet.") except IOError as e: print(f"Error reading scores from file: {e}") # Call the function to run the program manage_scores()
-
Open your terminal or command prompt.
-
Navigate to the directory where you saved
score_manager.py. -
Run the script using the Python interpreter:
python score_manager.py
-
Follow the on-screen prompts to enter names and scores.
score_manager.py: The main Python script containing the score management logic.scores.txt: (Automatically created) A text file where all the scores are stored. Each entry will be on a new line in the formatName: Score.