Is_prime strange error


#1



Link: https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096#

Error message:
Oops, try again. Did you create a function called is_prime? Your code threw a "global name 'total' is not defined" error.

I expect it to say well done move on instead.
P. S. I want to change my name from the generic data player but it doesn't let me change it on the account settings. Is this normal?


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


#2

in your function you use n, but n isn't defined anywhere?


#3

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

Now it returns this: Oops, try again. Does your is_prime function take exactly one argument (an integer)? Your code threw a "'int' object is not iterable" error.


#4

i should have said it differently, for your special cases here:

    if n < -1:
        return False
    elif n == 2:
        return True

n is not defined, you should use the function parameter

A function ends the moment a return keyword is reached, if the return keyword is reached in a loop, the loop simply breaks


#5

what is a function parameter? I thought I created a function


#6

x is your function parameter, it contains the number which you supply as argument on calling the function


#7

I still don't understand a thing. I'm struggling to figure out whats what and I've been stuck for hours. Just give me some clues of the correct code its making me tear my hair out. Thanks


#8

1) you use two loops, but there's only one thing to iterate through so what is the other loop doing?
2) your loops always exit the function on each iteration, so they never reach their second iteration, they aren't loops at all since they do not repeat more than once in total
3) an integer is not something you can iterate over, because it is a single value, not a collection of values
4) if you loop up to but not including x-1, then the last value will be x-2, which is a rather odd number to stop at


#9

Thanks for explaining it clearly


#10