A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 3 years ago
Someone please help me understand how this returns what it returns. The process. I kinda get the first part how variable which is 0 divided by 4 returns 0. but how come it doesnt show 0 for the second if statement and prints 4 first, then 4 then 12 then 16
for variable in range(20):
if variable % 4 == 0:
print variable
if variable % 16 == 0:
print ('foo!')
anonymous
 3 years ago
Someone please help me understand how this returns what it returns. The process. I kinda get the first part how variable which is 0 divided by 4 returns 0. but how come it doesnt show 0 for the second if statement and prints 4 first, then 4 then 12 then 16 for variable in range(20): if variable % 4 == 0: print variable if variable % 16 == 0: print ('foo!')

This Question is Closed

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0From http://docs.python.org/2/reference/expressions.html : "The % (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the ZeroDivisionError exception. The arguments may be floating point numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 4*0.7 + 0.34.) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand [2]." Now let's examine your programs output: 0 // 0 % 4 == 0 foo! // 0 % 16 == 0 4 // 4 % 4 = 0 8 // 8 % 4 == 0 12 // 12 % 4 == 0 16 // 16 % 4 == 0 foo! // 16 % 16 == 0 Which is indeed correct. The second statement, where variable has a value of 1, does not print any statements because both conditions fail: 1 / 4 == 0.25 1 / 16 == 0.0625 Does this make sense?

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0somewhat...the picture is a little clearer. I read your definition and understand somewhat. Do you think you can make your response more plain (python for dummies). I made the connection with the first 2 lines of output but the rest I still dont.

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0Have you tried calculating the third iteration of your loop by hand? So, you need to evaluate the statement: (2 % 4) == 0 In certain cases such as this, when you are working with a new operator it can be a good idea to create a test program. In this case you might want to calculate n % 4, given some value of n. This allows you to test your understanding and assumptions.

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0Something like, for variable in range(20): print str(variable) + ' % 4 = ' + str(variable % 4) print str(variable) + ' % 16 = ' + str(variable % 16) print '\n' for example.

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0Ok after running your example and studying it, I have a better understanding. Thanks for your help.
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.