Python functions - Practice Makes Perfect


#1

https://www.codecademy.com/courses/learn-python/lessons/functions/exercises/practice-makes-perfect?action=lesson_resume

Can anyone tell me why the code written below isn´t enough for the above listed exercise?

def cube(number):
return number**3

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


#2

this line:

if number/3==int:

won’t determine if the number is divisible by three. We can use the modulo operator to determine if a number is divisible by 3.

lets say number is 6, then 6 / 3 results in 2. then 2 == int, is false. 2 is of type integer, yes, but an integer doesn’t equal its data type


#3

Humm! Makes sense. Then I infer that i’d pass the exercise if I had put:

if type(number/3)==int

I can no longer tell through solving the exercise as I’ve already solved it using modulo. Thanks man


#4

You can always reset the exercise? Then you can submit again

how is this:

if type(number/3)==int

going to tell you if number is divisible by three? Your current if condition only tells of the result of division is of type integer.


#5

The result of the division of all and only those numbers divisible by 3 will be integers.

And yet it doesn´t work. I could write code that fixes the problem with an if condition so as to only include numbers equal or greater than 0 or 3, or use the simpler modulo, but it would not solve the question of how my thought process is wrong.

The code I wrote:

def cube(number):
return number**3

def by_three(number):
if type(number/3)==int:
return cube(number)
else:
return False

Won’t pass the exercise because "by_three(1) returned 1 instead of False"
I have just printed the type of 1/3 and it gave me int. I found that to be strange though sensible considering the outcome. Then I decided to print 1/3 and it results in 0. 2/3 as well. It seems as if it only tells me the integer result of the operation. How can I extend it to more digits? I know there must be some way of using %d, %s or, %f, i could probably write such code but it will be bothersome as H E L L!


#6

python will round down (floor) the result of divisions which involves two integers, you can’t rely on this approach to work, the modulo operator is clearly the better choice.

rounding down only occurs when the division involves two integers, so you could do number / 3.0, however, 3 / 3.0 is 1.0 which is a float, not an integer

in other words, you can’t rely on pythons division behaviour, the modulo operator is clearly the better approach


#7

I own python 3.6, and “1/3” runs perfectly fine there. However, def cube(number) gives me 1 (and I suspect more) syntax error/s. Why can´t I write programs on python 3.6 with the code that I learn here? Is it like khan-academy with their own version of JS? That would really suck…


#8

There are a few hacks around input and printing, but it’s otherwise the same as the python2 that you can download from python.org. I suspect khanacademy simply adds a few libraries, that it’s still “normal” js, it might even be running in your own browser. They do a bunch of drawing right? Because they want students to immediately see the effects of what you write.

Division always produces the same type, checking the type tells you nothing. In python 3 you’ll always get a float, in python 2 you’ll get an int if both operands are ints. Note that 5.0 is not an int. Yes, it is an integer value, but the type is float.

Also, you should avoid floating point operations unless you’re ok with losing precision, you may for example run into a case where dividing by three and then multiplying by three doesn’t result in the original value


#9

How should I code so that it will give me the result of a division to as many decimal places as I see fit?


#10

You’d basically put the digits in a list/array - which will be slower because you’re no longer using operations built into your cpu, but if you want a particular amount of precision, you can do it, and there’s even a module that does it for you - decimal
Note that no matter how many decimals you choose to keep, you’re still going to lose something when doing operations like division

Another strategy is to work with fractions. There’s a module for that too - fractions

The simplest strategy is to simply stick with integers when possible. For testing divisiblity, this is an obvious choice. So the answer to your question is that you most likely shouldn’t.


#11

no, codecademy runs python2. Python3 is not backward compatible with python2, division behaviour was one of the things changed in python3.

As you can see by our replies, the approach you want to choice really complicate matters, you would need to delve a lot deeper into division behaviour


#12

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