1

Just 5 days into Python, learning through Code Academy. I have no knowledge of any other language (very little knowledge of Ruby!).

What am I doing wrong with this code?


Q: Write a function, by_three, that calls a second function, cube, if a number is evenly divisible by 3 and "False" otherwise. You should then return the result you get from cube. As for cube, that function should return the cube of the number passed from by_three. (Cubing a number is the same as raising it to the third power).

So, for example, by_three should take 9, determine it's evenly divisible by 3, and pass it to cube, who returns 729 (the result of 9**3). If by_three gets 4, however, it should return False and leave it at that.

Lastly, call by_three on 11, 12, and 13 on three separate lines.

ANS:

def by_three(n):
    orig_num = n
    if (isinstance(orig_num, int) and orig_num%3 == 0 ):
        cube(orig_num)
    else:
        print "False"

def cube(orig_num):
    cube = orig_num**3
    print cube
    return

by_three(11)
by_three(12)
by_three(13)

When I run the above code, here is what I get. Why do these values appear in this way?

False
1728
False
==> None
False
False
1728
Oops, try again.
7
  • 1
    You should not use the same name for a function and for an internal variable. Commented Nov 23, 2012 at 5:46
  • @madth3 thanks. Did changes accordingly and still the same result! def by_three(n): if (isinstance(n, int) and n%3 == 0 ): cube(n) else: print "False" def cube(m): cube = m**3 print cube return by_three(11) by_three(12) by_three(13) Commented Nov 23, 2012 at 5:51
  • It looks like you may have some other code at work here (for instance, Python doesn't say 'Oops, try again' :) ). Although what you have doesn't quite match the requirements from a purely return perspective, it will return the correct result when run on its own. Commented Nov 23, 2012 at 5:53
  • yes. it was an "exercise" in codeAcademy. I did put in effort and since I couldn't understand the error, I'm asking you guys for help. Commented Nov 23, 2012 at 5:53
  • @RocketDonkey thanks. I'm using the codeacademy.com interpreter. Assume 'Oops, try again' is their doing. Still the question remains on why it is running twice. The first time, expected result appears and then it runs again, giving the wrong result. Commented Nov 23, 2012 at 5:56

3 Answers 3

1

I can't say why you're seeing odd results. When I copy your code into the interpreter, I see:

>>> def by_three(n):
...     orig_num = n
...     if (isinstance(orig_num, int) and orig_num%3 == 0 ):
...         cube(orig_num)
...     else:
...         print "False"
... 
>>> def cube(orig_num):
...     cube = orig_num**3
...     print cube
...     return
... 
>>> by_three(11)
False
>>> by_three(12)
1728
>>> by_three(13)
False

I think this problem is a lot simpler than you're making it, though. It's hard to tell because the question is rather poorly written, but this would be my answer:

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

def cube(n): return n**3

by_three(11)
by_three(12)
by_three(13)

And this is what it looks like in the interpreter:

>>> def by_three(n): return False if n % 3 else cube(n)
... 
>>> def cube(n): return n**3
... 
>>> by_three(11)
False
>>> by_three(12)
1728
>>> by_three(13)
False
Sign up to request clarification or add additional context in comments.

2 Comments

superb! thanks, man. I'm sorry about the "poorly written" question. That is how it was put in the "exercise". but, thanks! :)
Please do not use isinstance() - catch TypeError instead.
1

First you need to change the cube function to actually return the cube. And you can even simplify your cube method, by just returning the cube without storing the temporary result: -

def cube(orig_num):
    return orig_num**3   # Just return the cube

Then in your by_three function, rather than printing "False", you should return it. Also, return the value returned by cube function: -

def by_three(n):
    if (isinstance(n, int) and n % 3 == 0):
        return cube(n)  # Added return here
    else:
        return False  #Changed print to return.

You can also simplify this method to just a single line return statement. You should use try-except block instead of checking instance with isinstance, if you are passing a value in your function: -

def by_three(n):
    try:
        return cube(n) if n % 3 == 0 else False
    except TypeError, e:
        return False

And then, when you invoke the method, print the result obtained: -

print by_three(11)
print by_three(12)
print by_three(13)

1 Comment

Instead of isinstance() you should rather use try-except statements catching TypeError. In Python checking for type of passed value in similar cases is considered a bad practice.
0

In your cube method, you're not actually returning anything. Unlike Ruby, which returns the last statement in the block, you have to explicitly state what you're returning. There's also no need to create defensive copies of the value. Furthermore, you can't reuse the name cube, or that will clobber your method definition.

def cube(orig_num):
    return orig_num ** 3

Next, you would have to return the values in your caller method. I'll leave that as an exercise for you - shouldn't be too tricky to figure out from here.

Third, you (sort of) don't need to worry if the number is an int or a float. While there is imprecision with floating point numbers, the values shouldn't be perfectly divisible by 3.

Comments

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.