โ† Back to Python Course
๐ŸŽฎ Fun with Pythonยท25 minยทIntermediate
๐ŸŽฒ

Random & Games

Dice, luck, and guessing games!

Learn how to use the random module to build dice rollers, lucky number generators, and guessing games!

๐Ÿ“–

What we are learning

The random module lets Python make random choices. You can roll dice, pick random cards, generate lucky numbers, and build guessing games. Randomness makes programs unpredictable and fun!

๐Ÿ’ก Think of it like this:

Think of the random module like a bag of colored marbles. You reach in without looking and pull one out. You never know which color you will get โ€” and that surprise is what makes games exciting!

๐ŸŒŸ

Why it matters

Randomness is essential for games, simulations, and even security. Without randomness, every game would play the same way every time. Random makes things fun and unpredictable!

Where you see this in real life:

  • โœ“Dice rollers and card shufflers in digital board games
  • โœ“Random loot drops and enemy spawns in video games
  • โœ“Random password generators for security
๐ŸŽฏ

By the end, you can:

  • 1.Import and use the random module
  • 2.Use random.randint() for random integers
  • 3.Use random.choice() to pick from a list
  • 4.Build a number guessing game
๐Ÿ”ค

New words

importrandomrandintchoiceseed
๐Ÿ
If you could make Python pick one random thing for you right now, what would it be?
๐Ÿค“

Fun Fact!

Computers cannot actually be truly random โ€” they use complex math to generate "pseudo-random" numbers that are close enough for games and apps!

๐ŸŽฒ Random & Games

The randommodule lets Python make random choices. You can roll dice, pick random cards, generate lucky numbers, and build guessing games!

๐ŸŽฒ Rolling Dice

๐ŸRoll the dice!
main.py
โ— Output
Run it multiple times for different results!

๐ŸŽฏ random.choice()

random.choice()picks one random item from a list!

๐ŸPick random items from a list
main.py
โ— Output
Click โ–ถ Run to see the output!

๐ŸŽฎ Build a Guessing Game!

Let us build a number guessing game! Python picks a secret number, and you try to guess it. Python tells you if your guess is too high or too low.

๐ŸNumber guessing game!
main.py
โ— Output
Click โ–ถ Run to see the output!

๐Ÿš€ Challenge: Rock Paper Scissors!

Try building a Rock Paper Scissors game using random.choice()!

๐ŸRock Paper Scissors!
main.py
โ— Output
Click โ–ถ Run to see the output!
๐Ÿ
You are building real games now! ๐ŸŽฎ Try making the guessing game harder by changing the range to 1-1000!
๐Ÿง 

Quick Quiz!

+10 XP

What does random.choice(['a', 'b', 'c']) do?

๐Ÿ
Finished the lesson? Click the button to mark it complete and earn XP!