Let’s learn how to code a classic game in Python. We’re going to use conditionals, while loops, and random numbers to make a Rock, Paper, Scissors game where you play against the computer! Watch the tutorial video to see how we code this game step-by-step, and continue reading this post for more details.
Who is this project for?
This project info and learning outcomes summary will help you decide if this Python coding project is right for you.
This project falls under our Juni Python Level 2 coding class for kids. This intermediate Python tutorial is for students that want an Easy challenge project, about ~60 lines of code long. You should review print(), input(), and variables beforehand to get the most out of this project.
Some other projects you can try first for more practice with intermediate Python are our Python pet simulator and make a calculator in Python tutorials.
For learning outcomes, you’ll get a lot of practice with conditionals, while loops, and random numbers. This project is estimated to take you about , but you should move faster or slower at your own pace!
Before getting started, see how our finished project works for reference. Watch the video, or click run to see the project yourself!
You can also view my project solution code if you get stuck.
What to keep in mind before you start:
- You can play the game over and over until you choose to stop.
- The program keeps track of wins.
- Case doesn’t matter (i.e. ROCK is the same as rock).
- If you enter something invalid, the program keeps prompting you until you enter something valid.
Steps to Code the Project:
- Create a simple, one-round version of the game where we don’t enforce correct inputs.
- Add while loops to re-prompt the use to enter their choice if they type something invalid.
- Use a while loop to let the user play over and over, and use variables to keep track of the scores.
How do we do each of these steps?
Step 1: Create a simple, one-round version of the game where we don’t enforce correct inputs.
- Use random.randint() to generate a random number, and use conditionals to let each random number choose a different option for the computer (e.g. 0 mean rock, 1 means paper, 2 means scissors).
- Use input() to get the user’s choice.
- Use conditionals to see who wins.
Hint: Break it down into cases! If the user chooses rock, then there are possibilities depending on what the computer picked. You can use and, or nested conditionals!
Step 2: Add while loops to re-prompt the use to enter their choice if they type something invalid.
- Use .lower() on the user’s input to make sure our stored value is always lowercase.
- Write code so that while the user’s input is not one of the valid choices, it keeps prompting them to re-enter their choice.
Step 3: Use a while loop to let the user play over and over, and use variables to keep track of the scores.
- Use while True to create a loop that runs forever.
- After the end of the round, ask the user if they want to keep playing; if they say no, use break to end the loop.
- Use the same strategy from step 2 (.lower() and a while loop) to enforce a valid input for this choice too!
- Before the while True loop, create variables to keep track of the scores, and increment those variables in the right places.
Challenge Yourself with Extra Features
- Rewrite the program to be a two-player game instead of one player vs. the computer.
- Modify your random-number-generation and conditionals to change the probability of each choice for the computer!
Great job! Check out more coding tutorials
Thanks for watching and hope you had fun making this project with me! Every week, we’ll be posting project tutorials like this one, for different coding languages and experience levels, as well as math tutorials.
Check out our step-by-step coding projects to find our other tutorials in Python and more coding languages!
Need more help, or want to keep learning?
A Juni Instructor teaches basic Python to a student.
Looking up your coding questions is one of the best ways to learn! Another great way to learn is from an experienced coder or instructor.
Juni Learning Coding Instructors like Maya work closely with students ages 8-18, and are specially trained to adapt to each child’s unique learning style, pace, and interests.
Read more about our online coding classes for kids and curriculum, or contact our Admissions Team to learn which course is best for your student’s coding journey.
This article originally appeared on junilearning.com.