Assignment: Rock, paper, scissors

Great job learning about loops and functions, students! Now it's time to put your new knowledge to the test with a fun assignment.

For this assignment, you will create a program that plays the classic game Rock Paper Scissors. Here are the rules of the game:

  • The game is played by two players, who each choose one of three options: rock, paper, or scissors.
  • If both players choose the same option, the game is a tie.
  • Otherwise, rock beats scissors (because rock smashes scissors), scissors beats paper (because scissors cut paper), and paper beats rock (because paper covers rock).

Your program should ask the user for their choice and then randomly choose a choice for the computer. It should then determine the winner of the round and keep track of the score. The game should continue until the user decides to quit.

Example output

Chose an option below: 

0: Rock
1: Paper
2: Scissors

Please select a valid option above: 1

Player's choice: Paper
Computer's choice: Paper

It's a tie!

Try the game here!

Tips

Here are some tips to help you get started:

Step 1

Import the Python random module at the top

Code

Step 2

Create variable named choices that is an array of dictionaries that contain two values:

  • name (Name of the choice like, rock, paper or scissors)
  • beats (The name of the choice that it beats, for example, name=rock would contain beats=scissors)
Code

Step 3

  • Write a function called get_computer_choice that returns a random item from the choices dictionaries
    • Use random.randint(0, 2)
Code

Step 4

Create a menu that will list all the possible selections and their index (rock, paper, scissors). You can use this code to have access to the index value and choice.

  • for index, choice in enumerate(choices):
Code

Step 5

Prompt the user to select and index shown above and then assign the computer's choice to a variable using the get_computer_choice function

Don't forget that when getting the users input that the input() function will return a string. You'll need to cast that to an int using the int() function so you can use it to grab an index of choices.

Hint
Code

Step 6

Output the player's and computer's choice

Code

Step 6

Determine the winner by using the choice.get('name') and choice.get('beats')

Code

Solution

Here is the full code solution if you get stuck or if you have completed the challenge and want to see how I solved wrote the program.

I strongly encourage you to spend time trying to get this game working on your own before viewing this code solution. Remember, hard work always pays off!

Before you view the code!

But now that I have guilted you (lol), you can view the full code solution below

Full code solution

Server Academy Members Only

Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments

Saving Progress...

Sign up for free!

Sign up for free and get instant access to this course!.

Python 3 Fundamentals

0%

0/1 Lessons

Installing Python on Windows

• 1hr 17min

0 / 4 lessons complete

Python Basics

• 28min

0 / 7 lessons complete

Python Variables

• 41min

0 / 8 lessons complete

Even more Python Variables!

• 41min

0 / 6 lessons complete

Conditional Statements

• 15min

0 / 3 lessons complete

Writing Functions

• 30min

0 / 5 lessons complete

Python Loops

• 23min

0 / 5 lessons complete

Python PIP and Modules

• 18min

0 / 4 lessons complete

RegEx

• 26min

0 / 4 lessons complete

Working with APIs

• 12min

0 / 3 lessons complete

Course Conclusion

• 2min

0 / 1 lessons complete