🐍
PYTHON QUESTAgent Academy
0 XP
150 XP to Next Rank
Mission #10

🎲Lesson 10: Modules and Random Adventures

Add surprises, dice rolls, and timers!

+220 XP

Lesson 10: Modules and Random Adventures

Every great video game needs surprises and dramatic pauses! With Python Modules, we can import pre-made toolboxes like random (for dice rolls and loot) and time (for suspense).

Key Concepts to Remember:

  • •
    The import Keyword: Brings pre-built toolboxes into your file: import random and import time.
  • •
    random.randint(a, b): Generates a random whole number between a and b (like rolling a die).
  • •
    random.choice(list): Randomly selects one element out of any list.
  • •
    time.sleep(seconds): Pauses execution for dramatic suspense.

💻Rolling Dice and Picking Loot (IDE Syntax Colors)

import random
 
dice = random.randint(1, 6)
print(f"🎲 Rolled a {dice}!")
 
loot = ["Golden Sword", "Shield", "Ruby Gem"]
prize = random.choice(loot)
print(f"🎁 Found: {prize}")

💡 random.randint rolls a number, and random.choice picks an item from the list.

💡 Pro Tip: Always put your import statements at the very top of your Python file.