Make your own Rock-Scissor-Paper game with Python using Randomness
How to use Randomness in Python and create a Rock-Scissor-Paper game.
In this tutorial, you will:
- Explore Randomness in Python: Learn how to generate random numbers and utilize randomness in Python programming.
- Create a Rock-Scissor-Paper Game: Apply random number generation to develop a fun and interactive Rock-Scissor-Paper game, where players compete against the computer.
By the end of the tutorial, you will have a solid understanding of incorporating randomness into your Python programs. Additionally, you will have built a fully functional Rock-Scissor-Paper game, demonstrating your ability to combine random number generation and user interaction for an engaging gaming experience.
Step 1: Randomness in Python
To make games interesting you need some unpredictable aspects. This is where randomness is used.
Luckily, Python has a library to make randomness. To simulate rolling a die can be done as follows.
import random die = random.randint(1, 6) print(die)
Where randint(1, 6) returns a random integer from 1 to 6, both inclusive.
Step 2: The Rock-Scissor-Paper game
I think most know the game. If not, read the rules on the wiki.
Let’s try a game of it.
print("Enter choice \n 1. Rock \n 2. Paper \n 3. Scissor \n") choice = int(input("Choice: ")) computer_choice = random.randint(1, 3) if choice == computer_choice: print("Draw") elif choice == 1: if computer_choice == 2: print("Computer wins, Paper") else: print("You win, Scissor") elif choice == 2: if computer_choice == 1: print("You win, Rock") else: print("Computer wins, Scissor") elif choice == 3: if computer_choice == 1: print("Computer wins, Rock") else: print("You win, Paper")
It uses simple input statements to get input from the user. You can learn about input statements here. Then it converst the variable, which you can learn about here.
The new thing is using randomness in your Python code. This is what makes coding fun, it becomes unpredictable.
The rest is simple program flows with if-statements, which you can learn about here.
Step 3: What is Next?
In the next lesson you will learn Lists Basics to Create a Jumbled Game.
If this is something you like and you want to get started with Python, then this is part of an 8 hours FREE video course with full explanations, projects on each level, and guided solutions.
The course is structured with the following resources to improve your learning experience.
- 17 video lessons teaching you everything you need to know to get started with Python.
- 34 Jupyter Notebooks with lesson code and projects.
- A FREE eBook to support your Python learning.
See the full FREE course page here.
Python for Finance: Unlock Financial Freedom and Build Your Dream Life
Discover the key to financial freedom and secure your dream life with Python for Finance!
Say goodbye to financial anxiety and embrace a future filled with confidence and success. If you’re tired of struggling to pay bills and longing for a life of leisure, it’s time to take action.
Imagine breaking free from that dead-end job and opening doors to endless opportunities. With Python for Finance, you can acquire the invaluable skill of financial analysis that will revolutionize your life.
Make informed investment decisions, unlock the secrets of business financial performance, and maximize your money like never before. Gain the knowledge sought after by companies worldwide and become an indispensable asset in today’s competitive market.
Don’t let your dreams slip away. Master Python for Finance and pave your way to a profitable and fulfilling career. Start building the future you deserve today!
Python for Finance a 21 hours course that teaches investing with Python.
Learn pandas, NumPy, Matplotlib for Financial Analysis & learn how to Automate Value Investing.
“Excellent course for anyone trying to learn coding and investing.” – Lorenzo B.