Menu
Coddy logo textTech

Adding The Twist

Part of the Fundamentals section of Coddy's Python journey — lesson 52 of 77.

challenge icon

Challenge

Easy

Add a small twist to the game:

Numbers that contain the digit "3" but aren't divisible by 3 or 7 will output Almost Fizz

To check if a string contains a character, use the in membership operator — for instance, "a" in word returns True if "a" is found inside word.

This is not the same as the in keyword used in a for loop — here, in simply checks whether one value exists inside another (like checking if a digit exists inside a string).

Since in works on strings, you must first convert the number to a string using str(num) — for example, "3" in str(num) checks whether the digit 3 appears anywhere in the number.

Try it yourself

print("Welcome to FizzBuzz!")

def fizzbuzz(number):
    result = ""
    if number % 3 == 0:
        result += "Fizz"
    if number % 7 == 0:
        result += "Buzz"
    if result == "":
        result = str(number)
    return result

limit = int(input())

# Play FizzBuzz
for i in range(1, limit + 1):
    print(fizzbuzz(i))

All lessons in Fundamentals