Build a Pocket Dice Roller
By the end of this tutorial you'll know how to:
- Import Python modules
- Generate random numbers
- Store values in variables
- Display results to the user
- Create your first useful mobile tool
Why This Project Is Cool?
Many games use dice. Tabletop games, RPGs, board games, and even video games rely on random rolls. Today we're building our own digital dice roller inside Pythonista.
Step 1: Import Random
Python comes with a built-in module called "random".
Code
import random
This gives us access to tools that can make choices for us.
Step 2: Roll The Dice
Create a variable called roll.
Code
roll = random.randint(1, 6)
This tells Python: "Give me a random number between 1 and 6."
__________
__________
Step 3: Show The Result
Code
print("You rolled:", roll)
Now Python displays the number.
Step 4: run the script
Final Code
import random
roll = random.randint(1, 6)
print("You rolled:", roll)
Example Output
You rolled: 4
Run it again and you'll get a completely different number.
Challenge Mode ⭐
- Make the dice roll larger numbers.
- Make python roll multiple dice.