Python Control Flow
💡
Exercise 11

Coin Flip 10 XP Easy

Ctrl+Enter Run Ctrl+S Save

Welcome to Chapter 3: Control Flow! 🎯

💡 Think of it like this: Control flow is like a choose-your-own-adventure book. Based on a condition, your program takes different paths!

Python uses the random module to generate random numbers — perfect for simulating a coin flip! 🪙

import random # random.randint(a, b) gives a random number from a to b coin = random.randint(0, 1) if coin == 0: print('Heads!') else: print('Tails!')

How if/else works:

  • The if checks a condition — is it True or False?
  • If True → run the indented code below if
  • If False → skip to else and run that code instead
  • ⚠️ Don't forget the colon : after if and else!
  • ⚠️ The code inside must be indented (4 spaces)
📋 Instructions
Simulate a coin flip! 1. Import the `random` module 2. Generate a random number: `num = random.randint(0, 1)` 3. If `num` is `1`, print `Heads` 4. Otherwise, print `Tails` Since this is random, any output of `Heads` or `Tails` is correct!
Use: num = random.randint(0, 1), then if num == 1: print('Heads') else: print('Tails')
⚠️ Try solving it yourself first — you'll learn more!
import random

# random.randint(a, b) gives a random number from a to b
coin = random.randint(0, 1)

if coin == 0:
    print('Heads!')
else:
    print('Tails!')
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.