In this unit, you expanded your Python skills, and now you can really have some fun! In this activity, you are going to create a game that allows the user to play Rock, Paper, Scissors against the computer. If you aren’t familiar with this classic game, here’s how it works: two people (or a person and a computer!) each select either rock, paper, or scissors. The player who chooses the stronger object wins. Here is how the winner is determined:
Rock beats scissors because a rock can break scissors.
Paper beats rock because paper can cover a rock.
Scissors beats paper because scissors can cut paper.
Go to OnlineGDB to write your program. Your program should do the following:
Randomly choose rock, paper, or scissors for the computer.
Ask the user to choose rock, paper, or scissors.
Compare the computer’s choice to the player’s choice.
Announce whether the computer or the human won.
Here are two hints to help you write this program:
Test parts of your program to see if they work before moving on to work on other parts. For example, you will need to have the computer select a random number and then choose for that number to correspond to rock, paper, or scissors. You can write just that portion of the program and then test it by having the program print the word “rock,” “paper,” or “scissors” before you even begin working on the part of the program where the user inputs their choice. (Of course, don’t forget to erase the line of code that prints the computer’s choice when you are finished testing—otherwise the game will be just a bit too easy for the user!)
On a piece of paper, draw a chart that shows all nine possible outcomes of the game and use that chart to design and test the program. The chart might look like this:
11 answers
Rock vs Paper: Paper wins
Rock vs Scissors: Rock wins
Paper vs Rock: Paper wins
Paper vs Paper: Tie
Paper vs Scissors: Scissors wins
Scissors vs Rock: Rock wins
Scissors vs Paper: Scissors wins
Scissors vs Scissors: Tie
By using this chart, you can make sure your program covers all possible outcomes and correctly determines the winner.
```python
import random
def get_computer_choice():
choices = ["rock", "paper", "scissors"]
computer_choice = random.choice(choices)
return computer_choice
def get_user_choice():
user_choice = input("Choose rock, paper, or scissors: ")
return user_choice
def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "It's a tie!"
elif (user_choice == "rock" and computer_choice == "scissors") or \
(user_choice == "paper" and computer_choice == "rock") or \
(user_choice == "scissors" and computer_choice == "paper"):
return "You win!"
else:
return "Computer wins!"
def play_game():
computer_choice = get_computer_choice()
user_choice = get_user_choice()
print("Computer chooses:", computer_choice)
print("You chose:", user_choice)
result = determine_winner(user_choice, computer_choice)
print(result)
play_game()
```
You can copy and paste this code into an online Python interpreter like OnlineGDB and run it to play the game. Let me know if you have any questions!
```python
import random
def get_computer_choice():
choices = ["rock", "paper", "scissors"]
computer_choice = random.choice(choices)
return computer_choice
def get_user_choice():
user_choice = input("Choose rock, paper, or scissors: ")
return user_choice
def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "It's a tie!"
elif (user_choice == "rock" and computer_choice == "scissors") or \
(user_choice == "paper" and computer_choice == "rock") or \
(user_choice == "scissors" and computer_choice == "paper"):
return "You win!"
else:
return "Computer wins!"
def play_game():
computer_choice = get_computer_choice()
user_choice = get_user_choice()
print("Computer chooses:", computer_choice)
print("You chose:", user_choice)
result = determine_winner(user_choice, computer_choice)
print(result)
play_game()
```
Please copy and paste this code into an online Python interpreter and run it. Let me know if you encounter any issues or errors!
The code provided should work in both Python 2 and Python 3, but if you are explicitly required to use Python 3, you can run the code in a Python 3 interpreter without any issues. Just make sure to select Python 3 as the language when running the code.
If you encounter any specific errors or issues while running the code in Python 3, please let me know, and I'd be happy to help troubleshoot!
Here is the corrected code with proper indentation:
```python
import random
def get_computer_choice():
choices = ["rock", "paper", "scissors"]
computer_choice = random.choice(choices)
return computer_choice
def get_user_choice():
user_choice = input("Choose rock, paper, or scissors: ")
return user_choice
def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "It's a tie!"
elif (user_choice == "rock" and computer_choice == "scissors") or \
(user_choice == "paper" and computer_choice == "rock") or \
(user_choice == "scissors" and computer_choice == "paper"):
return "You win!"
else:
return "Computer wins!"
def play_game():
computer_choice = get_computer_choice()
user_choice = get_user_choice()
print("Computer chooses:", computer_choice)
print("You chose:", user_choice)
result = determine_winner(user_choice, computer_choice)
print(result)
play_game()
```
Make sure that each line within a function is indented properly with four spaces or a tab. Then try running the code again. If you encounter any more issues, please let me know!
If you encounter any further issues while running the code on OnlineGDB or have any questions, feel free to ask. I'm here to help!