Practice Makes Perfect


#1



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
------------break


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

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.


#2

which exercise exactly is this?

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


#3

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.


#4

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?


#5

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
            break
    else:
        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
            break
    else:
        return True

#6

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


#7

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.


#8

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


#9

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


#10

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


#11

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)


#12

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.


#13

Thank you for that, @ionatan


#14

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


#15

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


#16

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.


#17

Basically it would work.


#18

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.


#19

Thank you very much for that explaination.


#20

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