• Homework
    • 📝 Your Tasks (Pick 2)

Homework

Lets try hacking a script! This is the popular "FizzBuzz" example. FizzBuzz is one of the most common coding interview questions for entry-level software jobs. It looks simple, but it tests essential skills: - Can you write a loop correctly? - Do you know how to use if / elif / else conditions? - Can you apply logic with the modulo (%) operator? Employers like it because it quickly shows if a candidate can think logically and translate a problem into working code with a simple but efficient solution, we also look for elegant code when grading your assignment. Why Interviewers Ask FizzBuzz: - Weed out resumes vs. reality: Some people list “Python” on their resume but struggle to code basic loops. - Checks fundamentals: Loops, conditionals, operators. - Easy to explain: No complicated setup needed. Fun Fact: In the early 2000s, FizzBuzz gained popularity after several companies reported that many candidates couldn’t solve it — even with a degree in computer science!
# Print numbers from 1 to 20
# If divisible by 3 → print "Fizz"
# If divisible by 5 → print "Buzz"
# If divisible by both → print "FizzBuzz"

print("Expected Output:")
for num in range(1, 21):
    if num % 3 == 0 and num % 5 == 0:
        print("FizzBuzz")
    elif num % 3 == 0:
        print("Fizz")
    elif num % 5 == 0:
        print("Buzz")
    else:
        print(num)

📝 Your Tasks (Pick 2)

1. Loop with Step Size: Modify the loop so it doesn’t always go up by 1. For example, make it count by 2s, 3s, or another step size of your choice. Show how the FizzBuzz logic still works when skipping numbers.
2. Reverse a Loop: Change the loop so it prints numbers from 20 down to 1 instead of 1 up to 20. Keep the FizzBuzz logic the same, but make sure it still works correctly when looping backwards.
3. Loop with else: Add an else block to the loop. In Python, the else after a loop runs once when the loop finishes normally (not when broken).
4. One-Line Loops (List Comprehensions): Rewrite the FizzBuzz loop using a list comprehension instead of multiple if/else statements. The output can be stored in a list and then printed all at once.
# **Task 1**
for num in range(1, 21, 2):  # 1 to 20, skip every other number
    if num % 3 == 0 and num % 5 == 0:
        print("FizzBuzz")
    elif num % 3 == 0:
        print("Fizz")
    elif num % 5 == 0:
        print("Buzz")
    else:
        print(num)

1
Fizz
Buzz
7
Fizz
11
13
FizzBuzz
17
19
# **Task 2**
result = [
    "FizzBuzz" if n % 3 == 0 and n % 5 == 0
    else "Fizz" if n % 3 == 0
    else "Buzz" if n % 5 == 0
    else n
    for n in range(1, 21)
]

print(result)

[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz', 16, 17, 'Fizz', 19, 'Buzz']