5 Nisan 2022 Salı

FizzBuzz questions and answers

Write a code that lists the numbers from 1 to 50 and gives the outputs of

'fizz' if the number is a multiple of 3,
'buzz' if the number is a multiple of 5,
'fizzbuzz' if the number is a multiple of both 3 and 5,
or the number itself if the conditions above are not met.


for i in list1:
    if i%15==0:
        print('fizzbuzz')
    elif i%5==0:
        print('buzz')
    elif i%3==0:
        print('fizz')
    else:
        print(i)

If the question asks to write a function this may help as well:

def fizzbuzz(x):
    if x>50 or x<1:
        print('out of range')
    elif x%15==0:
        print('fizzbuzz')
    elif x%5==0:
        print('buzz')
    elif x%3==0:
        print('fizz')
    else:
        print(x)

Hiç yorum yok:

Yorum Gönder