6:Practice Makes Perfect


#1



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


My project is not returning False and I keep getting this error message: Oops, try again. by_three(4) returned 64 instead of False


I expected it to return false


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


#2

number / 3 doesn't return a bool/boolean but instead some number. I think you have to check if number is divisible by 3 with the modulo (%) operator.


#3

I didn't realize that- thanks! It worked :slight_smile:


#4

it didnt work for me


#5

What have you tried? Have you tried it with the modulo operator? :wink:


#6

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

Keeps returning 1 instead of false. Have no idea why.


#7

Try this.

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

else: 
    return False

cube(3)


#8

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


#9

Thank you. Was never good at arithmetic


#10

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

Mine problems on line 6


#11

You defined cube(number) then tried to define cube(number) again. In the lessen your second def should be by_three. Not sure where you got n in your 2nd, 4th and 5th line. should return number as defined in line one. Also number in your if statement. Your error is due to n not defined in your if so it is false and try's else. Else fails too since n is not defined.

Best I got to explain what happened. I'm no pro but I see the logic of the code is not sound.
Hope this helped.


#12

I have the same issue where it keeps returning 1 and not False. Why is it so? Is it part of the coding? Adding the n%3==0 solves the problem, but I don't understand why and how it solves it. Can you please provide an explanation?
Thanks!

This is the one that works:

def cube(number):
return number*number*number

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

This is the one that doesn't work and returns a 1 instead of a False:

def cube(number):
return number*number*number

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


#13

try this.
when defining the by_three(number) make sure that it is divisible by three by typing if cube( number) %3==0 meaning that the remainder of the cube divide by three will give you zero. This will work.


#14

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