Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 7 Online
OpenStudy (anonymous):

why this piece of code is not working as it suppose to do ?! #FizzBuzz for i in range (1,101): if i%3 ==0: print 'Fizz' if i%5 ==0: print 'Buzz' if i%3==0 or i%5==0: print 'FizzBuzz'

OpenStudy (osanseviero):

The first thing I see is the identation for the third block (when the number is divisible by 3 and 5). You want to check if it is div by 3 (print fizz), by 5 (print buzz), or by 3 and 5 (fizzbuzz), so the 3 if should be at the same lvl. Then you use i%3==0 OR i%5==0, you should be using and, not or. You want to make sure that both are true

OpenStudy (curry):

And to add on, you should check for (3 && 5) first and then 3 individually and 5 individually.

OpenStudy (curry):

or else you'll print somtehing like fizz, buzz, fizzbuzz all on the same line.

OpenStudy (anonymous):

or you could just remove the check for both entirely, since as it stands now it will print "Fizz" followed by "Buzz" to give "FizzBuzz" for numbers divisible by 15 (although you might want to print a newline afterwards)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!