Question #190202

Activity:

1. FizzBuzz

Create a program that will display numbers 1-31. When the number Is divisible by 3, it will print Fizz besides

the number. But when the number is divisible by 5, it will print buzz besides the number. But if the number is

divisible by 3 and 5, it will print FizzBuzz besides the number.

Sample output:

1

2

3fizz

4

5buzz

6fizz

7

8

9fizz

10buzz

11

12fizz

13

14

15fizzbuzz

16

17

18fizz

19

20buzz

21fizz

22

23

24fizz

25buzz

26

27fizz

28

29

30fizzbuzz




Expert's answer

for i in range(1,31):
    if i % 3 == 0:
        print("{}fizz".format(i))
    elif i % 5 == 0 and i % 5 == 0:
        print("{}fizzbuzz".format(i))
    elif i % 5 == 0:
        print("{}buzz".format(i))
    else:
        print(i)
LATEST TUTORIALS
APPROVED BY CLIENTS