Skip to main content
ThePythonBook/Assessment

Control Flow Assessment

Test your mastery of Python control flow: conditionals, loops, comprehensions, and more. Complete all 12 exercises to earn your certificate.

Progress
1000 XP0/12
#1FizzTwist
Write Code

Write a function fizz_twist(n) that returns a list of strings for numbers 1 through n (inclusive).

  • If a number is divisible by 3, append "Fizz"
  • If divisible by 7, append "Twist"
  • If divisible by both 3 and 7, append "FizzTwist"
  • Otherwise append the number as a string
  • Loading editor...
    #2Off-by-One Sum
    Fix the Bug

    The function sum_range(a, b) should return the sum of all integers from a to b inclusive. It currently has a bug. Fix it.

    Loading editor...
    #3Nested Conditionals
    Predict Output

    What does this code print? Enter the exact output (one value per line).

    x = 15
    if x > 20:
        print("high")
    elif x > 10:
        if x % 2 == 0:
            print("mid-even")
        else:
            print("mid-odd")
    else:
        print("low")
    Loading editor...
    #4Collatz Sequence
    Write Code

    Write a function collatz(n) that returns the Collatz sequence starting from n as a list. The sequence follows these rules:

  • If the current number is even, divide it by 2
  • If odd, multiply by 3 and add 1
  • Stop when the number reaches 1 (include 1 in the list)
  • Loading editor...
    #5Runaway Counter
    Fix the Bug

    This function should count down from n to 1 and return a list of the countdown values. But it runs forever. Fix the bug.

    Loading editor...
    #6Break and Continue
    Predict Output

    What does this code print? Enter the exact output.

    for i in range(1, 10):
        if i % 2 == 0:
            continue
        if i > 6:
            break
        print(i)
    Loading editor...
    #7Is It Prime?
    Write Code

    Write a function is_prime(n) that returns True if n is a prime number, False otherwise. Assume n is a positive integer. Numbers less than 2 are not prime.

    Loading editor...
    #8Comprehension Conversion
    Refactor

    Refactor the function below to use a single list comprehension instead of the for-loop. The function should still return the same result: a list of squared even numbers.

    Loading editor...
    #9Enumerate Surprise
    Predict Output

    What does this code print? Enter the exact output.

    words = ["cat", "dog", "fox"]
    for idx, word in enumerate(words, start=1):
        if idx == 2:
            continue
        print(f"{idx}:{word}")
    Loading editor...
    #10Staircase Pattern
    Write Code

    Write a function staircase(n) that prints a right-aligned staircase of height n using # characters. Each step is one # wider than the previous. Pad with spaces on the left so the total width is n.

    Example for n = 4:

    #
      ##
     ###
    ####

    The function does not need to return anything.

    Loading editor...
    #11Buggy Comprehension
    Fix the Bug

    This function should return words that have more than 3 characters, converted to uppercase. The current code has a bug that causes wrong output. Fix it.

    Loading editor...
    #12Matrix Diagonal
    Refactor

    Refactor the function below to use enumerate and a ternary expression inside a list comprehension. The function returns a flat list where diagonal elements (row index == column index) are kept and all other elements are replaced with 0.

    The current implementation is correct but verbose. Make it more Pythonic while keeping the same output.

    Loading editor...