thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

02.02 · Dice Rolling / Learn · 6 min

Explain Dice Rolling

strip removes surrounding whitespace; lower lets Y and y follow the same branch. n exits before any random value is generated. An invalid answer stays inside the loop and requests another choice.

Activity 2 of 2 in Dice Rolling

All courses

Python Challenge Workbook / 02.02

Dice Rolling review

Not started

Compare your approach with the original recording and an explained browser adaptation.

More options

Your lesson progress

Walkthrough: not startedPractice: not started

Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

Cartoon Rob reviews the game rules and valid or invalid outcomes for Dice Rolling.
Rob’s comic strip · Dice Rolling

ORIGINAL WALKTHROUGH · 05:31

Watch with Rob.

Pause whenever you want to try a change. You can return to the lesson at any time.

LEARN WITH THE CODE ZONE

Unlock this video walkthrough

Written lessons and coding practice are free. Video walkthroughs are included with a paid Code Zone membership or an active free trial.

Have a Code Zone website account? Use your learning account’s email to get a six-digit sign-in code, or use your existing website password.

Need a website account? Paid Skool members and active trial members can use the email from their membership questions to request a sign-in code. No invite link or Skool password needed.

Close this window to keep practicing. After signing in, you’ll return to this lesson.

Video walkthroughs are included with an active Code Zone membership. The written lesson and practice are free.

Open video & code editor (opens in a new tab)

Starter source code for VS Code

Copy the source below or download the file to follow along in VS Code.

The download includes a lesson link in each file and a README so you can find your way back.

main.py
main.pypython · example
# Code along: Dice Rolling review
# Build your own version as you follow the video.
import random

Watch the original solution

Try the challenge before reading this review. Watch Rob’s original 5:31 solution recording above. Use theater mode to watch and code side by side, or pop out the editor into its own window. Keep your challenge editor open in another tab to compare your own approach.

Normalize once, then branch

strip removes surrounding whitespace; lower lets Y and y follow the same branch. n exits before any random value is generated. An invalid answer stays inside the loop and requests another choice.

Two dice need two draws

Call randint(1, 6) once for die1 and once for die2. Reusing die1 would make every roll doubles. Both endpoints are possible because randint includes its upper bound.

Extend only after passing

Try y, Y, spaces around an answer, an invalid word, and n on the first prompt. Bonus: count rolls, print dice art or celebrate doubles without changing the required output until the checks pass.

One explained browser version

This is one valid approach. The challenge accepts other implementations that meet its observable game contract. Reading or copying this example does not award a pass or replace your saved work.

One possible game implementation
game.pypython · example
import random

while True:
    user_input = input("Do you want to roll the dice? (y/n) ").strip().lower()
    if user_input == "y":
        die1 = random.randint(1, 6)
        die2 = random.randint(1, 6)
        print(f"You rolled a {die1} and a {die2}.")
    elif user_input == "n":
        print("Goodbye")
        break
    else:
        print("Please enter 'y' or 'n'.")
Open workspace to paste this example (opens in a new tab)

Build it one step at a time

Prefer learning by typing? Work through short instructions, run the Python code and check each change before continuing. Your guided project saves separately from the challenge below.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

6 steps · Saved work · Instant feedback

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

Explain which branch runs for invalid input, a win, a loss and a normal next turn before moving on.