Lab, Foundations Projects
7 tasks, easy to hard: Predict, then run, Write it, Complete it, Fix it, Trace it, Refactor it, Mini challenge. Hints come one at a time and you can see each answer. Lab work is saved as practice and earns no XP.
Open the lessonYou can read every task below. Running, checking and saving them needs a free ID.
7 tasks
Task 01 .Type what the program will print, then run it to compare.
Type exactly what this program prints.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 02 .Solve it from the prompt, starting from an empty editor.
Write hint(guess, secret) that returns "correct", "higher" (the secret is bigger) or "lower". Print hint(4, 9), hint(12, 9) and hint(9, 9).
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 03 .Fill the marked gaps in the starter code.
Replace each ___ so the loop counts every guess, stops on a win and prints Won in 3.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 04 .The starter code has a bug. Repair it so the checks pass.
This game reports one attempt too few, and it compares text with a number. The guesses are typed answers, so they are strings. Fix it so it prints Won in 2 attempts.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 05 .Fill the table with the variable values, step by step.
The program finds 37 between 1 and 50 by always guessing the middle. Each row shows low, high and guess each time any of them changes, once all three exist.
secret = 37
low, high = 1, 50
guess = 0
while guess != secret:
guess = (low + high) // 2
if guess < secret:
low = guess + 1
elif guess > secret:
high = guess - 1Sign in or create a free ID to fill in and check this table.
Task 06 .Rewrite working code to the stated rule, keeping the same output. Rule: Use a for loop over guesses instead of an index and a while loop.
This loop walks the guesses with an index. Rewrite it as for guess in guesses: with no index variable. The output must not change.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 07 .A short task that combines this lesson with earlier ones.
Write play(secret, guesses, max_attempts=4) that prints a hint line for each guess (GUESS: higher, GUESS: lower or GUESS: correct), stops on a win, and stops after max_attempts guesses. It returns the number of attempts on a win, or None if the player lost. Then print the result of two rounds so the output is:
5: higher 9: correct Result: 2 1: higher 2: higher 3: higher 4: higher Result: None
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.