2020년 5월 13일 수요일

FizzBuzz Python - How to solve FizzBuzz Python exercise

what is FizzBuzz Python?

FizzBuzz Python Test (FizzBuzz Test) is also used to identify the basic capabilities of the progremers. It's a problem that tests understanding of problems rather than determining skills with complex and difficult codes, and in fact, many programmers make mistakes.

Condition 
1) Output numbers from 1 to 200. 
2) Multiples of 3 output "Fizz" instead of numbers. 
3) Multiples of 5 output "Buzz" instead of numbers. 
4) The common multiple of 3 and 5 is "FizzBuzz".

How to solve FizzBuzz Python?

If you make a program simply according to the above conditions, it is as follows.

    for i in range(1, 201):
    if i % 3 == 0 and i % 5 == 0:         ## It does not matter if you check the co-multiples of 3 and 5 and write them down as i % 15 == 0.
        print('FizzBuzz')
        continue
    elif i % 3 == 0:                             ## Check the multiple of 3.
        print('Fizz')
        continue
    elif i % 5 == 0:                             ## Check the multiple of 5.
        print('Buzz')
        continue
    print(i)                                     

The numbers from 1 to 200 will be printed using the for iteration statement, the multiples of 3 will be Fizz, the multiples of 5 will be Buzz, and the co-drain of 3 and 5 will be FizzBuzz. It's a very simple code, but you should be careful of the following.

- Caution points 
1. Since the number is printed in print(i) at the bottom, if condition statement should be continued.  -> If not included, the numbers will be printed together (Fizz 3, Buzz 5, FizzBuzz and 15). 

2. If i % 3 = 0 and i % 5 = 0: should be placed at the top of the if conditional statement.  -> If it is located in the middle or below, the drainage conditional statement of 3 or 5 above will be passed after it is established, so the co-drain conditional expression of 3 and 5 will not be executed.

댓글 없음:

댓글 쓰기