Practice Makes Perfect


I finished the program after two days of trying.

Why didn't this work:
(for loop i)
if x / i == int() and x != i:
------------return False

And this did?
(for loop i)
if x % i == 0 and x != i:
            return False

Since if the x / i = int() it means that left over is 0. I'm not sure why I couldn't write it like this.


which exercise exactly is this?

could you post both the full code of the working and not working code?


It is Practice makes it perfect: is_prime
I can not find code, since I moved on.
But the left of the code is irrelevant to this, since it wasn't working until I changed this.


here is the url:

the url is the same for everyone, then it checks for user, and loads your code, can you copy what you have in there to the forum?


Okay, this is the code:

def is_prime(x):
    n = range(2,x-1)
    if x < 2:
        return False
    elif x == 2:
        return True
    for i in range(2,x):
        if x % i == 0 and x != i:
            return False
        return True

And it didn't work with

def is_prime(x):
    n = range(2,x-1)
    if x < 2:
        return False
    elif x == 2:
        return True
    for i in range(2,x):
        if x / i == int() and x != i:    <----
            return False
        return True


ignoring for a second that int() is designed to convert numbers to an integer, and not really designed for what you do. (it works, but it is not really elegant)

okay, but in the working code you use the modulo operator (%) which calculates the remainder, in the not working code you use a division operator (/), they work very different


But if X % Y = 0 it is the same as X / Y = any int
Example: 5 % 4 = 1 , 5 / 4 = not int, it's float, 1,25
Example: 20 % 4 = 0, 20 / 4 = int, 5

Modulo operator = remainder of two numbers, if there is any.
And if there is any remainder division is not equal to integer.
If there is any remainder division equals float.


no, python2 will floor the number if needs to. so 5 /4 is 1, and that is an integer.

no? 20 is divisible by 4, so there is a remainder of zero.

dividing two integers and modulo operator can only give integers in return


In order to test whether something is equal to int(), the expression int() first has to be evaluated. It evaluates to 0, so 5 == int() evaluates to False because 5 is not equal to 0

In order to compare to a value's type you would need to first obtain the type. However the type will always be the same so that's not helpful anyway.

What you need to do is to test whether it divides without remainder


yes, the int() function will return zero if there is no value passed as argument. Which is why is why i don't understand he is not simply using zero


Because they want to test:
"is the result an integer?"

But that's not what
x == int()
Does at all - essentially it was written in hope that python would understand what was meant. But python doesn't guess what code means so that's not a viable strategy for writing code. @programmer_wannabe when you don't know, you have to go find out (as opposed to guessing)


Example: 20 % 4 = 0 ////,20 <<< this was another example, not 0,20 whether 0, 20/4

so it was 20 % 4 = 0 and 20 /4 = 5.
But the later response, that int() returns zero explains it.
Thank you, both of you.


Thank you for that, @ionatan


i should have added here, that if it can't convert, it returns zero which you can find in the documentation


And if I used just int w/o ()??


Then you would be comparing int to 5, are they identical?
What is int? Would 5 be equal to anything other than 5? Is int the value 5? What does the operator == do?

Why would it work the way you mean? You have to be able to motivate that.


Basically it would work.


Putting a pair of wings on a boulder isn't going to make it fly, no matter how clear the intent is.
Instead you've got to study the laws of nature and line things up just so in order to get something to fly.

You've got to consider what operations you have available to you and line them up in a way that amounts to the result you want.


Thank you very much for that explaination.


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