python - Keeping high scores in a text file

Python - Keeping high scores in a text file

To keep high scores in a text file using Python, you can follow these steps:

  1. Read High Scores from File: Read the existing high scores from the text file.

  2. Update High Scores: Update the high scores based on the current game scores.

  3. Write High Scores to File: Write the updated high scores back to the text file.

Here's a simple example demonstrating these steps:

import json def read_high_scores(filename='high_scores.txt'): try: with open(filename, 'r') as file: high_scores = json.load(file) except (FileNotFoundError, json.JSONDecodeError): high_scores = {} return high_scores def update_high_scores(current_scores, high_scores): for player, score in current_scores.items(): if player not in high_scores or score > high_scores[player]: high_scores[player] = score def write_high_scores(high_scores, filename='high_scores.txt'): with open(filename, 'w') as file: json.dump(high_scores, file) # Example usage current_game_scores = {'Player1': 150, 'Player2': 200, 'Player3': 180} # Step 1: Read high scores from file existing_high_scores = read_high_scores() # Step 2: Update high scores update_high_scores(current_game_scores, existing_high_scores) # Step 3: Write high scores back to file write_high_scores(existing_high_scores) 

In this example, high_scores.txt is a JSON file storing high scores. The read_high_scores function reads the existing high scores, the update_high_scores function updates them based on the current game scores, and the write_high_scores function writes the updated high scores back to the file.

Adjust the file name and paths based on your needs. This is a basic example, and you might want to add error handling and other features based on your specific requirements.

Examples

  1. "Python read high scores from a text file"

    • Description: Reading high scores from a text file in Python.
    • Code:
      def read_high_scores(file_path): with open(file_path, 'r') as file: high_scores = [line.strip() for line in file] return high_scores 
  2. "Python write high scores to a text file"

    • Description: Writing high scores to a text file in Python.
    • Code:
      def write_high_scores(file_path, high_scores): with open(file_path, 'w') as file: for score in high_scores: file.write(f"{score}\n") 
  3. "Python update high scores in a text file"

    • Description: Updating high scores in a text file in Python.
    • Code:
      def update_high_scores(file_path, new_score): high_scores = read_high_scores(file_path) high_scores.append(new_score) high_scores.sort(reverse=True) # Assuming high scores are sorted in descending order write_high_scores(file_path, high_scores[:10]) # Keeping the top 10 scores 
  4. "Python display high scores from a text file"

    • Description: Displaying high scores read from a text file in Python.
    • Code:
      def display_high_scores(file_path): high_scores = read_high_scores(file_path) for i, score in enumerate(high_scores, start=1): print(f"{i}. {score}") 
  5. "Python clear high scores in a text file"

    • Description: Clearing high scores in a text file in Python.
    • Code:
      def clear_high_scores(file_path): with open(file_path, 'w') as file: file.truncate(0) # Clearing the file content 
  6. "Python handle non-numeric entries in high scores"

    • Description: Handling non-numeric entries when reading high scores from a text file in Python.
    • Code:
      def read_high_scores(file_path): with open(file_path, 'r') as file: high_scores = [line.strip() for line in file if line.strip().isdigit()] return high_scores 
  7. "Python limit the number of high scores in a text file"

    • Description: Limiting the number of high scores to keep in a text file in Python.
    • Code:
      def write_high_scores(file_path, high_scores, max_scores=10): with open(file_path, 'w') as file: for score in high_scores[:max_scores]: file.write(f"{score}\n") 
  8. "Python remove duplicates from high scores in a text file"

    • Description: Removing duplicate high scores when reading from a text file in Python.
    • Code:
      def read_high_scores(file_path): with open(file_path, 'r') as file: high_scores = list(set(line.strip() for line in file if line.strip().isdigit())) return high_scores 
  9. "Python handle empty high scores file"

    • Description: Handling cases where the high scores file is empty in Python.
    • Code:
      def read_high_scores(file_path): try: with open(file_path, 'r') as file: high_scores = [line.strip() for line in file if line.strip().isdigit()] except FileNotFoundError: high_scores = [] return high_scores 
  10. "Python calculate average high score from a text file"

    • Description: Calculating the average high score from a text file in Python.
    • Code:
      def calculate_average_high_score(file_path): high_scores = read_high_scores(file_path) if high_scores: average_score = sum(map(int, high_scores)) / len(high_scores) return average_score else: return 0 

More Tags

street-address commando micro-frontend kendo-dropdown jvm .net-standard aws-iot foreign-keys pushviewcontroller laravel-socialite

More Programming Questions

More Fitness-Health Calculators

More Weather Calculators

More Other animals Calculators

More Various Measurements Units Calculators