Is_prime


#1



What is wrong with this code:
The error message says: Oops, try again. Does your is_prime function take exactly one argument (an integer)? Your code threw a "'int' object is not iterable" error.


def is_prime(x):
    for n in x:
        if n % 2 == 0 and x % n == 0:
            return False
        else:
            return True


#2

here:

for n in x:

x is a integer, integers are not iterable.


#3

Thanks for the fast reply, but I still am getting the same error message:

def is_prime(x):
    for n in is_prime:
        if n % 2 == 0 and x % n == 0:
            return False
        else:
            return True

#4

and how is looping over a function object better?

The problem is that x is an integer, how could we loop over numbers in the range between 2 and x?

remember we learned the range() function? (if you want, you can also use a while loop)

It is fine if you keep replying in this topic if you need more help, but you should learn something from this track, so don't post to quickly.


#5

def is_prime(x):
    for n in is_prime:
        if n == 2:
            return True
        elif n<2 or n % 2 == 0: 
            return False
        else:
            return True

This works for every thing but odd composite numbers- could you give me a clue on how to make it work for the odd composites..
Thanks


#6

you can't loop over a function object:

for n in is_prime:

what is the condition for a number to be a prime number?


#7

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