Stuck on 6. Practice Makes Perfect


#1



https://www.codecademy.com/courses/python-beginner-c7VZg/1/5?curriculum_id=4f89dab3d788890003000096#


My code returns 1 instead of False.


I expect it to return false but i do not know how


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


#2

to check a number is divisible by 3, number % 3 should equal zero. (i don't see the equals zero bit)

If the number is divisible by 3, return cube(number), not just number


#3

Ha right after i posted this i found it out. im not sure if im just reading it wrong or what but why does the number divided by 3 have to equal zero?


#4

Well, actually i was lying a bit for your own good. Look:

1 % 3 = 1
2 % 3 = 2
3 % 3 = 0
4 % 3 = 1
5 % 3 = 2
6 % 3 = 0
7 % 3 = 1
8 % 3 = 2
9 % 3 = 0

so when a number is divisible by three, the remainder is zero, which why you have to check it equal zero.

But here is the catch:

if 0:
   print 0
if 1:
   print 1
if 2:
   print 2

zero is considered false by python, while positive integers are considered true. In other words, if you don't use == 0, your function works exactly opposite as it should, of course we could 'reverse' this logic:

if number % 3:
    return False
else:
    return cube(number)

#5

Ah okay i didnt think itd register 0 as false, as to why i didnt think i needed == 0 lol


#6

Now you do know, do you understand it now?


#7

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