Practice Makes Perfect - Step 4


#1


I'm trying to justify the lines of this code to myself before moving on. I figured out thanks to a separate post that this is the correct code:

def cube(number):
return number**3
def by_three(number):
if number % 3 != 0:
return False
else:
return cube(number)

How can I know that the following instructions mean that I must set the code in Line 4 equal to zero?
Step 4: "if that number is divisible by 3, by_three should call cube(number) and return its result. Otherwise, by_three should return False."

Please help me understand the reasoning in knowing to write

if number % 3 != 0

instead of anything else.


Replace this line with your code.


#2

You're better off figuring out what actions your code should carry out, and then translating those actions one at a time into code.

You would want to consider how you would test if something is divisible by three. What makes such a number different from something not divisible by three, is there some behaviour there that can be tested? "divisible by three" sounds like you can just divide it and see if that's successful, no?

If you're asking what % does, then google python modulo operator


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.