FizzBuzz
This commit is contained in:
parent
b68cbd8a8c
commit
719e952863
1 changed files with 62 additions and 0 deletions
62
fizzbuzz.py
Normal file
62
fizzbuzz.py
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
''''
|
||||
FizzBuzz
|
||||
|
||||
You are going to write a program that automatically prints the solution
|
||||
to the FizzBuzz game. These are the rules of the FizzBuzz game:
|
||||
|
||||
|
||||
Your program should print each number from 1 to 100 in turn and include number 100.
|
||||
|
||||
|
||||
But when the number is divisible by 3 then instead of printing the number it
|
||||
should print "Fizz".
|
||||
|
||||
|
||||
When the number is divisible by 5, then instead of printing the number it
|
||||
should print "Buzz".`
|
||||
|
||||
|
||||
And if the number is divisible by both 3 and 5 e.g. 15 then instead of the number it
|
||||
should print "FizzBuzz"
|
||||
|
||||
|
||||
e.g. it might start off like this:
|
||||
|
||||
1
|
||||
2
|
||||
Fizz
|
||||
4
|
||||
Buzz
|
||||
Fizz
|
||||
7
|
||||
8
|
||||
Fizz
|
||||
Buzz
|
||||
11
|
||||
Fizz
|
||||
13
|
||||
14
|
||||
FizzBuzz
|
||||
|
||||
...etc
|
||||
|
||||
'''
|
||||
# 1st solution
|
||||
for number in range(1, 101):
|
||||
if number % 15 == 0:
|
||||
print("FizzBuzz")
|
||||
elif number % 3 == 0:
|
||||
print("Fizz")
|
||||
elif number % 5 == 0:
|
||||
print("Buzz")
|
||||
else:
|
||||
print(number)
|
||||
|
||||
# 2nd less effort, more efficient style
|
||||
for n in range(1, 101):
|
||||
output = ""
|
||||
if n % 3 == 0:
|
||||
output += "Fizz"
|
||||
if n % 5 == 0:
|
||||
output += "Buzz"
|
||||
print(output or n)
|
||||
Loading…
Add table
Add a link
Reference in a new issue