Kids Coding

Random Surprises

import random. Pick a number, a word, or a choice so each run is different.

KidsIntermediateRandom Surprises

Make each run different

import random loads a toolbox. Then you can pick a number or an item.

A dice

import random

print(random.randint(1, 6))
print(random.randint(1, 6))

import random goes at the very top of the file, once. Forget it and Python says name 'random' is not defined.

Pick from a list

choice

import random

pets = ["cat", "dog", "fox"]
print(random.choice(pets))

A secret number

This is the missing piece from the Basic guess game.

Random secret

import random

secret = random.randint(1, 5)
guess = 3
print("Secret was", secret)
if guess == secret:
    print("Lucky")
else:
    print("Not this time")

Real life

Picking who goes first in a board game.

A die is random. randint is a die the computer rolls for you.

Who starts

import random

players = ["Sam", "Riley", "Jun"]
print("Starts:", random.choice(players))
print("Dice:", random.randint(1, 6))

Change one word. Run it. That is how this idea shows up outside the lesson.

Try it

Exercise 1: Print a random colour from ["red", "green", "blue"].

Show solution
import random
print(random.choice(["red", "green", "blue"]))

FAQ: Random Surprises

Common questions about this page.

What is StudyGrid Kids?

StudyGrid Kids is a free coding path for children: Basic, Intermediate, and Advanced. Lessons use Python in the browser. No account and no install.

Should I run random surprises examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn random surprises in this Kids Coding Python lesson (Random Surprises).

Which kids level should we start with?

Start at Basic if the child has never written code. Choose Intermediate if they can print, use variables, and write a simple if. Choose Advanced if they already use loops and lists.

Do kids need an account or an app?

No. Kids pages are public. Code runs in the Try Python editor at /try. We do not ask children for a name or an email.

What language do kids learn?

Python, with a first HTML page in Intermediate and Advanced. After Advanced, the main Python tutorial is the next step.

Is the kids coding track free?

Yes. The Kids home, the three levels, and the Python editor on StudyGrid (studygrid.in) are free.