Python
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Data Types
-
Section 3: Collections
-
39: Set Operations: Union, Intersection, Difference
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Turtle Graphics and Early Practice Projects
-
Section 7: Working with Files and I/O
-
Section 8: Regular Expressions
-
Section 9: Object-Oriented Python
-
Section 10: Error Handling
-
Section 11: Modules and Packages
-
Section 12: Iterators, Generators, and Functional Tools
-
Section 13: Decorators and Metaprogramming
-
Section 14: Concurrency and Parallelism
-
Section 15: Working with Dates, Times, and Numbers
-
Section 16: Standard Library Deep Dive I: Data Structures
-
Section 17: Standard Library Deep Dive II: System and Introspection
-
Section 18: Standard Library Deep Dive III: Security and Encoding
-
Section 19: Standard Library Deep Dive IV: Text and Data Utilities
-
Section 20: Networking and Web Basics
-
Section 21: Working with Databases
-
Section 22: Testing and Quality
-
Section 23: Advanced Typing
-
Section 24: Context Managers and Resource Handling
-
Section 25: Text, Unicode, and Binary Data
-
Section 26: More Functional and Iteration Tools
-
Section 27: Data Validation and Configuration
-
Section 28: Working with Images and Media
-
Section 29: Property-Based and Documentation Testing
-
Section 30: Packaging and Deployment
-
Section 31: Performance and Internals
-
Section 32: Design Patterns in Python
-
Section 33: GUI Programming
-
Section 34: Security Basics
-
Section 35: Data Structures and Algorithms
-
Section 36: Practical Projects
-
Section 37: Capstone Projects
-
Section 38: Interview and Algorithm Practice
-
Section 39: Writing Idiomatic Python
431: Building a Dice Rolling Simulator
How do I actually get a random number that feels like a die?
When you're building a simulator, the first thing you'll reach for is the random module. Now, you have a few choices here, but for a dice roll, random.randint(a, b) is your best friend. The reason is simple: unlike most Python functions where the stop value is exclusive (like range()), randint is inclusive.
If you want a standard six-sided die, you use random.randint(1, 6). This ensures you can actually hit that 6, which would be a problem in a game of Yahtzee if your code only ever gave you 1 through 5.
import random
def roll_die():
return random.randint(1, 6)
print(f"You rolled a: {roll_die()}")
How do I keep the simulator running without restarting the script?
If you just run the code above, it rolls once and dies. That's annoying. In the real world, we handle this with a while loop. I usually prefer a while True loop combined with a specific "exit" keyword. It makes the program feel like an actual application rather than a one-off script.
You'll want to capture user input inside the loop. I like to use .lower() on the input just in case the user types "QUIT" instead of "quit"βit's a small touch, but it prevents the program from crashing or ignoring the user just because of a Shift key.
import random
while True:
user_input = input("Press Enter to roll or type 'q' to quit: ").lower()
if user_input == 'q':
print("Thanks for playing!")
break
result = random.randint(1, 6)
print(f"π² You rolled a {result}!")
What if I want to support different types of dice, like a D20?
Hardcoding the number 6 is fine for a basic example, but if you're building this for a tabletop RPG, you're going to need flexibility. The trick here is to stop treating the die as a fixed object and start treating it as a parameter in a function.
I recommend creating a function that accepts the number of sides as an argument. This way, the same logic handles a 4-sided die, a 20-sided die, or even a 100-sided die without you having to write three different functions. I've seen beginners write separate functions for roll_d6() and roll_d20()βdon't do that. Keep it DRY (Don't Repeat Yourself).
import random
def roll_custom_die(sides):
if sides < 1:
return "A die must have at least one side!"
return random.randint(1, sides)
# Now you can just call it with whatever you need
print(f"D20 Roll: {roll_custom_die(20)}")
print(f"D4 Roll: {roll_custom_die(4)}")π Practical Task
Build a Tabletop RPG Multi-Dice Roller
Instead of rolling a single die, create a program that allows a user to simulate multiple dice of the same size at once (e.g., "3d6" meaning three six-sided dice).
- Ask the user for the number of dice they want to roll.
- Ask the user for the number of sides each die should have.
- Use a loop to generate the rolls and store them in a list.
- Print each individual roll and the final total sum of all dice.
- Wrap the entire process in a loop so the user can keep rolling until they type 'exit'.
There are no comments for now.