-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathFizzBuzz.py
More file actions
29 lines (26 loc) · 861 Bytes
/
FizzBuzz.py
File metadata and controls
29 lines (26 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Goal: Write a program that prints the numbers from 1 to 50 but:
# - For multiples of three print “Fizz” instead of the number.
# - For the multiples of five print “Buzz” intead of the number.
# - For numbers which are multiples of both three and five print “FizzBuzz” intead of the number.
#
# There are many ways to solve this but here is two alternatives:
# Using string concatenation:
for i in range(1, 51):
string = ""
if i % 3 == 0:
string = string + "Fizz"
if i % 5 == 0:
string = string + "Buzz"
if i % 5 != 0 and i % 3 != 0:
string = string + str(i)
print(string)
# Using if, elif and else
for i in range(1, 51):
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)