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

Good morning guys. Concerning to FizzBuzz problem (Recitation #01), I tried to solve it before I could read the answer. Here is my code: http://dpaste.com/hold/1423427/ I did not understand very well how the program concatenated Fizz and Buzz when the number was a multiple of both 3 and 5. That's why I worked on three conditions, in my code. Can anyone help me understanding it? Thanks.

OpenStudy (anonymous):

``` elif x%3 ==0 and x%5==0: print 'FizzBuzz' ``` This will print FizzBuzz when x is an even multiple of 3 and 5 The other two conditions will not. What is it you don't understand? % is the modulo operator http://en.wikipedia.org/wiki/Modulo_operation

OpenStudy (anonymous):

Hey, thanks. Actually I did not understand the code the professor gave us for concatenating multiples of 3 and 5.

OpenStudy (anonymous):

Do you understand it now? If not, post it.

OpenStudy (anonymous):

Actually no, I did not understand. The gaven code has the following commands: ``` for i in range (1,101): s=str(i) if i%3==0 or i%5==0: s='' if i%3==0: s=s+'Bizz' if i%5==0: s=s+'Fuzz' print s else: print s ``` I did not catch how it concatenates multiples of both 3 and 5 with the code above. Thanks.

OpenStudy (anonymous):

The way it concatenates is by adding to the existing value of s each time the condition is met in the two if statements, instead of replacing it each time. It starts by setting s to a blank string in line 4, then adds each word separately depending on which number it is. If it's both, then it adds "Fuzz" after "Bizz" has been added, because both 'if' statements can run one after the other as they are not 'elif' statements. Hope that helps.

OpenStudy (anonymous):

Try his and see if it helps. http://tinyurl.com/lrojtft

OpenStudy (anonymous):

Once this statement returns TRUE - if i%3==0 or i%5==0: It will execute BOTH if statements underneath it. If they both evaluate to TRUE, then s will get BIZZ + FUZZ.

OpenStudy (anonymous):

Thanks everybody, I shall close this question.

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!