←Previous page

12GAME PROJECTS · MAGIC 8-BALL

Shake the magic ball.

Ask a yes-or-no question and get a surprise answer, picked by chance.

IN REAL LIFE

Answers picked by chance.

These toys feel like magic, but they only pick by chance.

The Magic 8-Ball toy

You shake it, and one of 20 answers floats up in the window.

Fortune cookies

Each cookie hides a message. Which one you get is luck.

Paper fortune tellers

You pick colors and numbers, then open a flap with a funny answer.

Just a game: The ball cannot see the future. random.choice picks an answer by chance, like pulling a paper from a hat.

THE PLAN

Four small steps.

Each step uses something you already learned.

The answers

A list keeps every answer the ball can give.

[ ] append

Shake the ball

A function picks one answer with random.choice and returns it.

def random.choice return

Ask again and again

A while loop keeps asking with input() until the player types bye.

while input() !=

Say goodbye

After the loop, print a goodbye. It always runs last.

print()

RUN IT

Ask the ball.

Guess first. Then run real Python and check.

1

Guess the result

The answers are random, but some things are sure. Look at PyBot's answers.

PyBot's answers: two questions, then bye

How many times does Python show "The ball says:"?

magic_ball.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Run it twice. The answers may change, but the number of answers stays the same.

BUILD IT

Build the ball, piece by piece.

The answers are random, so these goals check things that are always the same.

0Completed
11Not tried
0Review
01

Step 1: more answers

The ball only has two answers. Add "Maybe." to the list.

Goal output
['Yes!', 'No way.', 'Maybe.']
True
OUTPUT

02

Step 2: give the answer back

shake() picks an answer but never gives it back, so reply is None. Fix the function.

Goal output
True
OUTPUT

03

Step 3: keep asking

PyBot fixed the answer to test the loop. It should answer until bye, but it stops at once. Fix the while.

Goal output
Your question: Will I fly?
The ball says: Maybe.
Your question: bye
Bye!
OUTPUT

YOUR TURN

Questions about the ball.

Try each tiny question. A wrong answer becomes something to review.

04

What does choice do?

random.choice(answers)

What does this give back?

05

Why return?

def shake(): return random.choice(answers) print("The ball says:", shake())

What does return do here?

06

Bye or bye?

while question != "bye":

The player types: Bye

Does the loop stop?

07

Ask before the loop

question = input("Your question: ") while question != "bye":

Why is there an input() before the while?

08

Can it see the future?

The ball says: Yes!

Will it really happen?

09

Same question twice

You ask "Will I win?" two times.

Will the ball give the same answer?

10

Something sure

In the big game, the player types bye at the end.

Which line can you be sure Python shows last?

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

Chance makes games surprising.

random.choice picks from a list, a function returns the pick, and while keeps the game going. It cannot see the future.