• 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**
# Loop twith Step Size
for i in range(1, 21, 2):
    if i % 3 == 0 and i % 5 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)
1
Fizz
Buzz
7
Fizz
11
13
FizzBuzz
17
19
# **Task 2**
# Reverse a Loop
for i in range(20, 0, -1):
    if i % 3 == 0 and i % 5 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)

Buzz
19
Fizz
17
16
FizzBuzz
14
13
Fizz
11
Buzz
Fizz
8
7
Fizz
Buzz
4
Fizz
2
1
# **Task 3**
# Loop with else
for i in range(1, 21):
    if i % 3 == 0 and i % 5 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)
else:
    print("Loop finished successfully!")
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Loop finished successfully!
# **Task 4**
# One-Line Loops
fizzbuzz_list = [
    "FizzBuzz" if i % 3 == 0 and i % 5 == 0 else
    "Fizz" if i % 3 == 0 else
    "Buzz" if i % 5 == 0 else
    i
    for i in range(1, 21)
]
print(fizzbuzz_list)

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