How can we design a simple game in Python

 How can we design a simple game in Python that allows the user to play rock, paper, scissors against the computer?

 

 Here's an explanation of each line of code in the Rock, Paper, Scissors game                                  

1. import random                                                    


This line imports the random module, which will be used later in the script to generate random numbers for the computer's choice in the game.

 

2. print("Welcome to Rock, Paper, Scissors!")

 

This line displays a welcome message to the player, introducing the game.

 

3. while True:

 

This line starts a while loop that will run until the player decides to quit the game by entering 0. The True value in the while statement means that the loop will run indefinitely until the break statement is encountered.

 

4. print("\nEnter choice \n1 for Rock \n2 for paper \n3 for scissors \n0 for Quit")

 

This line displays the options for the player's choice in the game: rock, paper, scissors, or quitting the game.

 

  5.  choice = int(input("Your turn: "))

 

This line asks the player to make their choice by entering an integer (1 for rock, 2 for paper, 3 for scissors, or 0 to quit). The player's choice is stored as an integer in the choice variable.

 

   6. while choice > 3 or choice < 0:

        choice = int(input("Enter valid input: "))

 

This while loop validates the player's input to make sure it's within the acceptable range (0 to 3). If the player's input is not within the acceptable range, the loop will run again and prompt the player to enter a valid choice.

 

 7.   if choice == 0:

        print("\nThanks for playing!")

        break

 

If the player's choice is 0, this if statement will display a message thanking the player for playing, and the break statement will stop the while loop, causing the program to exit.

 

8.    comp_choice = random.randint(1,3)

 

This line generates a random integer between 1 and 3 to represent the computer's choice in the game. The random.randint function is used to generate this random integer, and it's stored in the comp_choice variable.

 

9.    if comp_choice == 1:

        comp_choice_name = 'Rock'

    elif comp_choice == 2:

        comp_choice_name = 'Paper'

    else:

        comp_choice_name = 'Scissors'

 

 

This series of if statements maps the computer's choice (stored as an integer in comp_choice) to a string representation (rock, paper, or scissors) stored in the comp_choice_name variable.

 

 

10.    print("Computer chose: " + comp_choice_name)

 

This line displays the computer's choice in the game.

 

 

11.    if (choice == 1 and comp_choice == 1) or (choice == 2 and comp_choice == 2) or (choice == 3 and comp_choice == 3):

        print("Draw")

    elif (choice == 1 and comp_choice == 2) or (choice == 2 and comp_choice == 3) or (choice == 3 and comp_

 

Entire code:

import random

 

print("Welcome to Rock, Paper, Scissors!")

 

while True:

    print("\nEnter choice \n1 for Rock \n2 for paper \n3 for scissors \n0 for Quit")

   

    choice = int(input("Your turn: "))

   

    while choice > 3 or choice < 0:

        choice = int(input("Enter valid input: "))

   

    if choice == 0:

        print("\nThanks for playing!")

        break

    else:

        comp_choice = random.randint(1,3)

        if comp_choice == 1:

            comp_choice_name = 'Rock'

        elif comp_choice == 2:

            comp_choice_name = 'Paper'

        else:

            comp_choice_name = 'Scissors'

           

        print("Computer chose: " + comp_choice_name)

       

        if (choice == 1 and comp_choice == 1) or (choice == 2 and comp_choice == 2) or (choice == 3 and comp_choice == 3):

            print("Draw")

        elif (choice == 1 and comp_choice == 2) or (choice == 2 and comp_choice == 3) or (choice == 3 and comp_choice == 1):

            print("Computer wins")

        else:

            print("You win")

 

This code implements the classic game of Rock, Paper, Scissors, where the user enters their choice of rock, paper, or scissors and the computer randomly selects its choice. The outcome of the game is then displayed, with either the computer, the user, or the game being declared the winner.

 

Post a Comment

0 Comments