0% found this document useful (0 votes)
12 views

SRTHK

The document describes the code for a basic Rock Paper Scissors game between a user and computer. It imports the random module to generate the computer's random choice, defines a list of options, prompts the user for their input and stores it and the computer's random choice in variables. It then prints the choices and uses conditional statements to compare them and determine a winner by printing messages like "You win!" or "You lose." It provides optional additions like playing multiple rounds by prompting the user.

Uploaded by

rbh51816
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

SRTHK

The document describes the code for a basic Rock Paper Scissors game between a user and computer. It imports the random module to generate the computer's random choice, defines a list of options, prompts the user for their input and stores it and the computer's random choice in variables. It then prints the choices and uses conditional statements to compare them and determine a winner by printing messages like "You win!" or "You lose." It provides optional additions like playing multiple rounds by prompting the user.

Uploaded by

rbh51816
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Importing the random module:


Python

import random

Use code with caution.

content_copy

This line imports the random module, which provides functions for generating random numbers. This
will be used to simulate the computer's choice in the game.

2. Defining options:

Python

options = ["Rock", "Paper", "Scissors"]

Use code with caution.

content_copy

This line creates a list named options that stores the three possible choices in the game: "Rock",
"Paper", and "Scissors".

3. User input:

Python

user_choice = input("Choose Rock, Paper, or Scissors: ")

Use code with caution.

content_copy

This line prompts the user to enter their choice using the input function. The user's input is stored in
the user_choice variable.

4. Computer's choice:

Python

computer_choice = random.choice(options)

Use code with caution.

content_copy

This line uses the random.choice function from the imported random module. It selects a random
element from the options list and assigns it to the computer_choice variable. This simulates the
computer's random selection.

5. Displaying choices:

Python

print("You chose:", user_choice)

print("Computer chose:", computer_choice)


Use code with caution.

content_copy

These lines print both the user's choice and the computer's choice to the console.

6. Determining the winner:

This part uses conditional statements (if, elif, and else) to compare the user's choice with the
computer's choice and determine the winner:

Python

if user_choice == computer_choice:

print("It's a tie!")

elif user_choice == "Rock":

if computer_choice == "Scissors":

print("Rock smashes scissors! You win!")

else:

print("Paper covers rock! You lose.")

elif user_choice == "Paper":

if computer_choice == "Rock":

print("Paper covers rock! You win!")

else:

print("Scissors cuts paper! You lose.")

elif user_choice == "Scissors":

if computer_choice == "Paper":

print("Scissors cuts paper! You win!")

else:

print("Rock smashes scissors! You lose.")

else:

print("Invalid input. Please choose Rock, Paper, or Scissors.")

Use code with caution.

content_copy

The first if statement checks for a tie (when both choices are the same).

The following elif statements handle each possible user choice:

If the user chooses "Rock", it checks the computer's choice and prints the corresponding message
("win" for scissors, "lose" for paper).
Similar logic applies for "Paper" and "Scissors" choices.

The final else statement handles invalid user input.

7. (Optional) Playing again:

You can add functionality to play multiple rounds. This can involve prompting the user if they want to
play again and resetting the game loop.

This explanation provides a basic understanding of the code. You can find more elaborate versions
online that include features like:

Keeping score

Adding a loop to play multiple rounds

Improving error handling for invalid user input

Here are some additional points to consider:

The code can be made more concise using a dictionary to map user choices to winning conditions.

Error handling can be improved to provide more informative messages for invalid user input.

You might also like