0

I have a Python code like this,

def cube(n):
    n = n * n * n
    return n

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

Currently I am not able to return any value, please let me know what is the issue?

5
  • 2
    please revisit the indentation, it seems wrong. (don't define functions inside functions except you know what you are doing). Also: How do you call the functions? Commented Feb 20, 2016 at 21:17
  • In cube(), you can simply say def cube(n): return n ** 3 Commented Feb 20, 2016 at 21:39
  • After fixing the indentation, this code works. Is there a question, or should this be posted to codereview? Commented Feb 20, 2016 at 21:58
  • 1
    @helloV please do not change the indentation of code unless the OP specifies that it should be changed. In your edit, you changed the entire scope of the question. Commented Feb 20, 2016 at 22:02
  • Oops. sorry @MattDMo. Now I notice my mistake. It was done in a haste. Commented Feb 20, 2016 at 22:04

1 Answer 1

6

You do not set the value of cube(n) in your by_three function to return.

def cube(n):
    result = n ** 3 # You can use ** for powers.
    return result

def by_three(n):
    if (n % 3 == 0):
        print "number is divisible by three"
        result = cube(n)
        return result
    else:
        print "number is not  divisible by three"
        return False

I am also assuming that you have made a mistake in your indentation when copying to SO. If that is not the case, and that is your original indentation, then you will need to rectify that also.

Sign up to request clarification or add additional context in comments.

3 Comments

Or just return n ** 3.
Thanks @Ffisegydd this solved my problem also I understood the function definition and calling
or you could just say return n ** 3 or return cube(n) (save memory space)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.