6/19


#1

Hello. I recently picked up wtih Python and have been having difficulty's with 6/19 of Practise makes perfect. . I did all previous lessons reasonable quick, sometimes with a lil struggle but google fixs that :D. But after looking at it i didnt really understanded what to do,

My code was starting with

def cube(number):
if (number**3)
return cube
def by_three

and after that i didnt knew what to do. So i looked up at someone other code and it showed me this

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

def by_three(number):
if (number) % 3 == 0:

     else:
        return: False

Now my question is what did i do wrong and could someone explain me again why the % is there and 3 == 0 ?

I'm pretty new into Python. Just at a 23% at the curse. Never progammed anything before! so really could use the help of you guys.


#2

Well your cube equation is returning the function it's self which is called a recursive function and you will get an infinite loop if it passes your if condition.

The other two functions do what their name states, the first one returns the cube of the number passed to it. Based on the code you have the other ones does nothing, it is incorrectly written.

Good Code:

def cube(number):
    return number**3

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

Now on to what they do.

Some reading

Operators in Python:Same thing on different site

The % operator is called a modulus and what it does is remainder division. Easy enough?

So,

3 % 2 = 1
3 % 2 == 0 # This will evaluate to False

This makes the % a handy flow control as they are using it in the by three function. It says if this number is divisible by 3 with no remainder return True.

number % 3 == 0

That should wrap that up any questions ask.


#3

I still dont get this code tho.

like what is : number**3 . why is there the word number instead of a legit number?.

also why is a "==" added to it and a 0 ? number % 3 == 0 .

Thanks for your answers. im really new to this

English isnt my first languange so please dont use expensive words :smiley:


#4

@acermma

The reason for all thoe things is because of what they mean in python.

= assigns a value to the left variable
== compares left to right and returns True/False if they are equal
+, -, /, //, %, * these are all basic math symbols
** means to the power of ie exponent

So when we do,

def cube(number):
    return number**3

The word immediately following the def is the what the function's namespace is going to be equal to.

After the def cube what is in the () is know as the parameters of the function. They are used like a variable, except they are not predetermined before the function is used.

The next line after def cube(number): starts with return, what this line does is returns the value that followed the return command. If it is an equation it will compute it first then return the value.

After the return we have an equation so the computer will do that before returning a value. The equation is number**3 or number to the power of 3.

Now that we know all the different parts of our function, let's put give it a number to work with.
In python you simply invoke the namespace of the function you wish to use along with the variables and parameters you wish t pass to it in ().

cube(5)

Invoking the code above we should get the output of

125

Also, you most likely can find documentation in your native language. If you don't understand something google it and read up on it. If you need further guidance go ahead and post here with a more specific question.


#5

Hey. I do know most of the operators .But what i dont get is the code. Why does it work since number isnt really a number. So what does the power of 3 work with? I have no problem continueing coding. I just really have issue's with 6/19. I also dont get a simple thing bout number % 3 == 0 ??. Shouldnt that be False since 3 isnt equal with 0?


#6

zeziba already answered this nicely, but to summarize, number is a parameter. A function takes any numeric value that you pass to it via the parameter and does whatever you told it to do in the function's body.

I also had trouble understanding this concept when starting out programming, but maybe it would help to think of a parameter as a variable that you feed into a function at runtime (not necessarily user input, but in this exercise it is that way).

If there was just an actual "legit number", say 48 for example, as opposed to the number parameter, your code would not be very reusable because that numeric value would be hard-coded into your function.

zeziba also answered this very well, but to summarize again, there is a difference between the assignment operator "=" and the comparator "==". The assignment operator takes whatever variable you have on the left side of it and assigns it the value you specify to the right of it. When you use the comparator "==" this symbol tells the program or script that you want to compare the two values you specify on either side of the comparator to determine whether to return True or False based on if the condition has been met or not.To put it another way, (= != ==).

So, to put everything together:
The expression "number % 3 == 0" means "take any value for the number parameter that I pass to this statement, divide that value by 3, and then compare that value to the value on the right side of the comparator." In this case, if the value of the number parameter divided by 3 gives a remainder of 0, the statement returns True.

Hope that's not too confusing.


#7

basically this works

def cube(number):
return number**3

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

your welcome


#8

This is hard to explain, but basically the % sign gives you the remainder after you divide two numbers. For example, 10 % 3 will return 1 (10 % 3 == 1) because after you divide 10 by 3, 1 is your remainder. The == sign just means equals when you are using operations on numbers. Two equal signs are used because = is what is used to define a variable.
The reason that 'number % 3 == 0' is true is because it is something that needs to be true for the 'if' statement to run; it isn't a statement in itself. 'number' is your parameter, and part of the 'if' statement. So what it is really saying is, 'if a number divided by three gives me a remainder of 0, return ___.'
I hope that helps. Let me know if you want me to explain it differently.