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 fromcube. As forcube, that function should return the cube of the number passed fromby_three. (Cubing a number is the same as raising it to the third power).So, for example,
by_threeshould take 9, determine it's evenly divisible by 3, and pass it to cube, who returns 729 (the result of 9**3). Ifby_threegets 4, however, it should returnFalseand leave it at that.Lastly, call
by_threeon 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.
returnperspective, it will return the correct result when run on its own.