Practice Makes Perfect


#1

I'm not the only one in my calss with this problem. Where's the blooper?

def cube(number):
def by_three(number):
if number 3 == 0:
return "n is divisible by 3"
else:
return "n is not"

if n % 3 == 0:
return "n is divisible by 3"

else:
return "n is not"


#2

I think something went wrong copy-pasting the code into the forum post, because that doesn't make any sense at all;) Try to get code to show up correctly and I'm sure someone can give you an answer in a couple of minutes. (Four spaces before pasting the code or something like that)


#3

Hey @arraycoder64136, could you try posting your code again? I'd love to help you, but it's not very readable in that format. To post code properly click the button that looks like </> and then paste the code.


#4

@arraycoder64136
one blooper here is that its best to implement these function separately like below

#the blooper is here you ought to implement these function separately
def cube(number):
    return number**3 if type(number) in [float,int] else None

def by_three(number):
    return cube(number) if not number %3 else False
    
print (cube(6))
print (by_three(9))
print (by_three(5))

but according to what you implemented above it looks like you wanted to implement it via closures then you do something like this

def by_three(number):
    def cube(n):
        n = number

        # a bit overkill here but just to show a point
        return n**3 if not number %3 else False
    return cube

t1 = by_three(9)
t2 = by_three(5)
print (t1(9))
print (t2(5))