I want to convert a loop-based script containing if
, elif
and else
into a list comprehension but I cannot figure out how to do it.
Here is the script I wrote (it prints the numbers from 1 to 100, but for multiples of 3 it prints 'fizz', for multiples of 5 it prints 'buzz' and for multiples of both 3 and 5 it prints 'fizzbuzz'):
for num in range(1, 101):
if num % 3 == 0 and num % 5 == 0:
print('fizzbuzz')
elif num % 3 == 0:
print('fizz')
elif num % 5 ==0:
print('buzz')
else:
print(num)