Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to do FizzBuzz challange with python?

We have x number that goes 0 to 100. if x number divisible by 3 write ‘fizz’, if divisible by 5 write ‘buzz’ and if divisible by 3 and 5 write ‘fizzbuzz’.

What is wrong with this code below?

Code for FizzBuzz:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i = 1

while i <= 100:
 i = i + 1
 
 if (i % 3 == 0):
     print('Fizz')
 elif  (i % 5 == 0):
     print('Buzz')
 elif (i % 3 == 0) and (i % 5 == 0):
     print('FizzBuzz')
 else: print(i)
     

>Solution :

Welcome! This might help you out:

i = 1

while i <= 100:
 answer = ''
 
 if (i % 15 == 0): print('FizzBuzz')
 elif (i % 3 == 0): print('Fizz')
 elif (i % 5 == 0): print('Buzz')
 else: print(i)
 
 i = i + 1

Basically, after you assigned a to 1, you instantly add another 1 in your while loop, so I moved your i = i + 1 to the bottom of the while loop.
Also, your 'FizzBuzz' wouldn’t be printed out – you skip if it divides both on 3 and 5, it divides by 15, but you skip that making fizzes and buzzes show in the first place and skipping the FizzBuzz. I fixed that too.

I hope it helped!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading