Python

 

 

In this tutorial, we will create a simple digital scoreboard to track the points in a ping pong game. This program will allow us to increment and display the scores of two players, simulating a live scoreboard for the game. It will be a fun and simple Python project to practice basic programming concepts such as loops, conditions, and user input handling.

Objective

The main objective of this project is to build a Python-based digital scoreboard for a ping pong game. The scoreboard will have the ability to:

  • Track and display scores for Player 1 and Player 2.
  • Allow users to increment scores for each player with a key press.
  • End the game when a player reaches a pre-defined winning score.

Python Code

# Ping Pong Scoreboard Program

def display_score(player1_score, player2_score):
    print("\n--- Ping Pong Scoreboard ---")
    print(f"Player 1: {player1_score} | Player 2: {player2_score}")
    print("-----------------------------")

def main():
    player1_score = 0
    player2_score = 0
    winning_score = 11

    print("Welcome to Ping Pong!")
    print(f"First player to reach {winning_score} points wins.\n")

    while player1_score < winning_score and player2_score < winning_score: display_score(player1_score, player2_score) # Ask for user input to update scores print("Press '1' to score for Player 1, '2' for Player 2, or 'q' to quit.") choice = input("Enter your choice: ").strip().lower() if choice == '1': player1_score += 1 elif choice == '2': player2_score += 1 elif choice == 'q': print("Game Over. You quit the game.") break else: print("Invalid input. Please try again.") # Declare the winner if player1_score >= winning_score:
        print("\nPlayer 1 wins the game!")
    elif player2_score >= winning_score:
        print("\nPlayer 2 wins the game!")

    display_score(player1_score, player2_score)

# Run the game
if __name__ == "__main__":
    main()

Explanation of the Program Structure

The program is structured in a simple and intuitive way:

  • display_score: A function that prints the current scores of both players in a readable format.
  • main: The main game function. It initializes the scores, manages user input, and controls the game loop.
  • The game loop continues as long as neither player reaches the winning score. Players can increment their scores by typing ‘1’ or ‘2’, and they can quit by pressing ‘q’.
  • The game ends when either player reaches the winning score (11 points by default), and the winner is announced.

How to Run the Program

  1. Ensure you have Python installed on your system. You can download Python from here.
  2. Copy the Python code provided above into a text file and save it with a ‘.py’ extension (e.g., ping_pong_scoreboard.py).
  3. Open your command line or terminal and navigate to the directory where the file is saved.
  4. Run the program by typing the following command: python ping_pong_scoreboard.py
  5. Follow the on-screen instructions to play the game.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)