What’s one thing you learned? What’s still confusing?
Mini-Project: FizzBuzz
Solve software's most famous interview problem — 1 to 100 with Fizz, Buzz, and FizzBuzz.
Functions: Reuse, Scope & First-Class Citizens
Define functions, master LEGB scope, *args/**kwargs, default arguments, lambdas, and closures.
Mini-Project: Tip Calculator
Build a tip + tax + split calculator with default parameters and tuple returns — your first reusable mini-library.
Interactive Labs for This Track
Loop Visualizer
You're a factory robot repeating the same task on an assembly line — watch how loops automate repetitive work
List Slicing
You have a playlist of 50 songs — grab just tracks 10 through 20 with a single slice expression
Sorting Algorithms
You're organizing a library of 10,000 books — which sorting method is fastest?
Ask questions, share insights
A guessing game where the computer secretly picks a number between 1 and 100 and your job is to find it. After each guess, the program tells you whether you went too high or too low. When you finally land on the right number, it tells you how many tries it took.
I picked a number between 1 and 100. Can you guess it?
Your guess: 50
Too high! Try again.
Your guess: 25
Too low! Try again.
Your guess: 37
Too high! Try again.
Your guess: 31
You got it in 4 tries!
random.randintwhile loopif/elif/elserandom module ships with Python -- no install needed. random.randint(1, 100) returns a random integer between 1 and 100, inclusive.import random
secret = random.randint(1, 100)
print(f"(Debug: the secret is {secret})")Print the secret for now so you can verify the rest of the logic. We'll remove that line at the end.
How is random.randint(1, 6) different from range(1, 6)?
range(a, b) excludes b (so range(1, 6) gives 1, 2, 3, 4, 5), but random.randint(a, b) INCLUDES both ends (so it can return 1, 2, 3, 4, 5, OR 6). If you want a random index for a list of length 6, use random.randint(0, 5) or — better — random.randrange(6), which matches range's exclusive-end behavior.What's the range of `random.randint(1, 100)`?
input() always returns a string, so you have to call int() if you want to compare it to a number.This code calls input(). Python here can't pause to ask you, so type the answers below before running — one value per line, in the order the program asks for them.
guess = int(input("Your guess: "))
print(f"You guessed {guess}")int() will crash with a ValueError. We'll ignore that for now and add validation in a later lesson on error handling.What happens at runtime? secret = 42 guess = input("Guess: ") # user types 42 print(guess == secret)
42, but input() always gives back a STRING — so guess is "42" (the string), not 42 (the integer). When Python compares "42" == 42, it returns False because a string and an int are never equal, even when they look identical. No crash — just a silently wrong answer. The fix: guess = int(input("Guess: ")). This is why every number-from-input pattern has that int() call.if/elif/else.if guess < secret:
print("Too low! Try again.")
elif guess > secret:
print("Too high! Try again.")
else:
print("You got it!")guess = secret to make sure the "got it" branch works, then with a wrong guess to make sure the high/low branches work.Why use `elif` instead of two separate `if` statements?
while True loop runs forever -- unless you break out of it. Combine that with the comparison from Step 3 so the program keeps asking until the user wins.while True:
guess = int(input("Your guess: "))
if guess < secret:
print("Too low! Try again.")
elif guess > secret:
print("Too high! Try again.")
else:
print("You got it!")
breakbreak immediately exits the closest loop. Without it, your game would run forever even after the user won.What does this print? for i in range(5): if i == 2: break print(i) print("done")
0, then 1. When i reaches 2, the if condition matches and break fires BEFORE the print — so 2 is never printed. The loop ends entirely, and execution jumps to the line after the loop, printing "done". Compare with continue, which would skip just the print for i == 2 and keep looping (output: 0 1 3 4 done). Break = leave the room. Continue = skip this turn.Track how many guesses the user took with a counter. Increment it on every guess. Display it when they win.
This code calls input(). Python here can't pause to ask you, so type the answers below before running — one value per line, in the order the program asks for them.
import random
secret = random.randint(1, 100)
tries = 0
print("I picked a number between 1 and 100. Can you guess it?")
while True:
guess = int(input("Your guess: "))
tries += 1
if guess < secret:
print("Too low! Try again.")
elif guess > secret:
print("Too high! Try again.")
else:
print(f"You got it in {tries} tries!")
breakThat's the full game. Optimal play with binary search wins in 7 tries or fewer for any number between 1 and 100 -- see if you can spot why.
Build a game where the computer picks a number from 1 to 100. The user keeps guessing -- you tell them higher/lower after each guess. When they win, print how many tries it took. Bonus: rate them ('Lucky!' for under 7, 'Solid!' for 7-10, 'Keep trying!' for 11+).
I picked a number between 1 and 100. Can you guess it?
Your guess: 50
Too high! Try again.
Your guess: 25
Too low! Try again.
Your guess: 37
Too high! Try again.
Your guess: 31
You got it in 4 tries! Lucky!import random
# TODO: pick a secret number between 1 and 100
# TODO: initialise the try counter
print("I picked a number between 1 and 100. Can you guess it?")
# TODO: while-True loop that asks for a guess
# - increment counter
# - if too low: print 'Too low!'
# - if too high: print 'Too high!'
# - if correct: print 'You got it in N tries!' and break
# TODO: after the loop, print rating based on tries
Try one of these variations: