Python Control Flow
💡
Exercise 14

Magic 8 Ball 15 XP Easy

Ctrl+Enter Run Ctrl+S Save

The Magic 8 Ball is a toy that gives random answers to yes/no questions. Let's build one!

We'll combine random.randint() with if/elif/else to pick a random response.

import random num = random.randint(1, 9) if num == 1: print('Yes, definitely.') # ... add more responses
📋 Instructions
Build a Magic 8 Ball! 1. Import the `random` module 2. Generate a random number between 1 and 9 3. Use `if`/`elif`/`else` to print one of these responses: - 1: `Yes, definitely.` - 2: `It is decidedly so.` - 3: `Without a doubt.` - 4: `Reply hazy, try again.` - 5: `Ask again later.` - 6: `Better not tell you now.` - 7: `My sources say no.` - 8: `Outlook not so good.` - 9: `Very doubtful.`
Use random.randint(1, 9) to get a random number. Then use if/elif/else with different ranges to give different fortunes. Think of a Magic 8 Ball!
⚠️ Try solving it yourself first — you'll learn more!
import random

num = random.randint(1, 9)

if num == 1:
    print('Yes, definitely.')
# ... add more responses
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.