Adding The Twist
Part of the Fundamentals section of Coddy's Python journey — lesson 52 of 77.
Challenge
EasyAdd 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
inmembership operator — for instance,"a" in wordreturnsTrueif"a"is found insideword.
This is not the same as theinkeyword used in aforloop — here,insimply checks whether one value exists inside another (like checking if a digit exists inside a string).
Sinceinworks on strings, you must first convert the number to a string usingstr(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
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 48Loops
For LoopWhile LoopBreakContinueRecap - FactorialThe Range FunctionNested LoopRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators9Functions
Declare a FunctionArgumentsReturnRecap - Sigma FunctionRecap - Validation FunctionDefault Values12Iterating Over Sequences
Iterating Over ElementsThe Enumerate FunctionIterating Over Strings Part 1Iterating Over Strings Part 2