Extend FizzBuzz so that a new rule needs only one new line.
What to build
A version with no long chain of conditions.
Requirements
- Add
Fizzfor multiples of 3,Buzzfor multiples of 5 andBangfor multiples of 7. - Combine the words in that order when several rules apply.
- Print the number if none of them apply.
Input
One whole number n.
Output
n lines.
Constraints
n is from 1 to 200.
Example
Input:
7
Output:
1
2
Fizz
4
Buzz
Fizz
Bang
Why it works
Independent if statements let the words add up, which handles every combination. With an elif chain, you would need a separate case for each combination.
Examples
Example 1 · Input
7
Expected output
1 2 Fizz 4 Buzz Fizz Bang
Example 2 · Input
2
Expected output
1 2
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Build the word piece by piece, with one separate if for each rule.
Hint 2
If the word is still empty at the end, use the number.
Solution
Show one correct solution (try it yourself first)
n = int(input())
for i in range(1, n + 1):
word = ""
if i % 3 == 0:
word += "Fizz"
if i % 5 == 0:
word += "Buzz"
if i % 7 == 0:
word += "Bang"
print(word or i)