Learn Python from Scratch: Basic Concepts and Building Your First Game

Python 101: From Basics to Building a Rock, Paper, Scissors Game

Hello, fellow programming enthusiasts! Welcome to my first blog post. I'm excited to share my journey of learning Python from scratch. Over the past few weeks, I've explored Python fundamentals like variables, data types, loops, and functions. Inspired by Bro Code on YouTube, I challenged myself to code a simple Rock, Paper, Scissors game. In this post, I'll explain my learning process and the game code step-by-step. Let's dive in!


Coding the Rock, Paper, Scissors Game

Here’s the GitHub link to the complete code for the Game: GitHub repository.

Code Explanation

  1. Importing the Random Module:

     import random
    

    The random module is used to make random selections, which is crucial for simulating the computer's choice in the game.

  2. Setting Up the Game Loop:

     while True:
    

    The while True: loop keeps the game running until the player decides to stop.

  3. Defining Choices:

     choices = ['rock', 'paper', 'scissors']
    

    We define a list of choices that both the player and the computer can choose from.

  4. Computer's Random Choice:

     computer = random.choice(choices)
    

    The random.choice() function randomly selects an option from the choices list for the computer.

  5. Player's Choice:

     player = None
    
     while player not in choices:
         player = input("rock, paper or scissors? : ").lower()
    

    We initialize player to None and use a while loop to ensure the player enters a valid choice. The .lower() method converts the input to lowercase to match the choices list.

  6. Game Logic:

     if player == computer:
         print(f'computer: {computer}')
         print(f'player: {player}')
         print(f'Tie!')
    
     elif player == 'rock':
         if computer == 'paper':
             print(f'computer: {computer}')
             print(f'player: {player}')
             print(f'You lose!')
         if computer == 'scissor':
             print(f'computer: {computer}')
             print(f'player: {player}')
             print(f'You win!')
    
     elif player == 'paper':
         if computer == 'rock':
             print(f'computer: {computer}')
             print(f'player: {player}')
             print(f'You Win!')
         if computer == 'scissor':
             print(f'computer: {computer}')
             print(f'player: {player}')
             print(f'You lose!')
    
     elif player == 'scissors':
         if computer == 'paper':
             print(f'computer: {computer}')
             print(f'player: {player}')
             print(f'You Win!')
         if computer == 'rock':
             print(f'computer: {computer}')
             print(f'player: {player}')
             print(f'You lose!')
    

    This section of code compares the player's choice with the computer's choice and determines the winner.

  7. Play Again Prompt:

     playAgain = input("continue (Yes/No)? : ").lower()
     if playAgain != "yes":
         break
    

    After each game, the player is asked if they want to continue. If the player enters anything other than "yes," the game loop breaks, and the game ends.

  8. Ending the Game:

     print('Bye, see you soon!')
    

    A friendly message is displayed when the game ends.

Conclusion

Writing this Rock, Paper, Scissors game was a rewarding experience that solidified my understanding of Python basics. I encourage all beginners to take on small projects like this to practice and apply what they've learned. Stay tuned for more posts as I continue my Python journey and explore more advanced topics. Happy coding!