โ† Back to Python Course
๐Ÿค” Making Decisionsยท25 minยทBeginner๐Ÿ† Project
๐ŸŽฑ

Mini-Project: Magic 8-Ball

Ask a question, get a mysterious answer!

Build a Magic 8-Ball that gives random answers to your questions using if/else, comparisons, and randomness!

๐Ÿ“–

What we are learning

Time for your second project! A Magic 8-Ball is a toy that gives random answers to yes-or-no questions. You will build one using variables, input, if/else, and a random number generator. Ask it anything!

๐Ÿ’ก Think of it like this:

The real Magic 8-Ball is a plastic ball with a 20-sided die floating in liquid. You shake it, and one of 20 answers floats to the top. Your digital version will do the same thing โ€” but with code!

๐ŸŒŸ

Why it matters

This project brings together decisions, randomness, and user input. It shows you how to build something interactive that responds differently every time.

Where you see this in real life:

  • โœ“Random answer generators and fortune tellers
  • โœ“Dice rollers and coin flip simulators
  • โœ“Games that need random events or loot drops
๐ŸŽฏ

By the end, you can:

  • 1.Use the random module to generate random numbers
  • 2.Combine if/elif/else with random choices
  • 3.Build an interactive program with input()
๐Ÿ”ค

New words

randomrandintimportelif
๐Ÿ
If you had a real Magic 8-Ball, what question would you ask it right now?
๐Ÿค“

Fun Fact!

The original Magic 8-Ball was invented in 1950 and has 20 possible answers: 10 positive, 5 negative, and 5 non-committal!

๐ŸŽฑ Build a Magic 8-Ball!

A Magic 8-Ball gives random answers to yes-or-no questions. You will build one using the randommodule, if/elif/else, and input().

๐ŸŽฒ The random Module

First, we need to import the random module. Then we can userandom.randint(a, b)to get a random number between a and b.

๐ŸTry random.randint()
main.py
โ— Output
Run it multiple times โ€” you get different results!

๐Ÿ—๏ธ Step 1: Generate a Random Answer Number

We will pick a random number from 1 to 5. Each number will correspond to a different answer.

๐ŸStep 1: Random number
main.py
โ— Output
Click โ–ถ Run to see the output!

๐Ÿ—๏ธ Step 2: Turn Numbers into Answers

Now use if/elif/else to turn each number into a fun answer!

๐ŸStep 2: The complete Magic 8-Ball!
main.py
โ— Output
Click โ–ถ Run to see the output!

๐Ÿš€ Challenge: Add More Answers!

Try adding more answers! Change randint(1, 5) to randint(1, 8) and add more elif branches.

๐ŸYour turn โ€” add more answers!
main.py
โ— Output
Click โ–ถ Run to see the output!
๐Ÿ
Awesome! You built a working game! ๐ŸŽ‰ You used random, if/elif/else, and input all together!
๐Ÿง 

Quick Quiz!

+15 XP

What does random.randint(1, 6) do?

๐Ÿ
Finished the lesson? Click the button to mark it complete and earn XP!