While loop for is_prime


#1



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


I am getting an error saying local variable 'a' is referenced before assignment. I assume this is because I am defining 'a' as another variable x.


I am trying to use a while loop to create my program. I want to set 'a' equal to x and then test x/(a-1) for divisibility and finally decreasing a by 1.


def is_prime(x):
    x=a
    while a>=1:
        if x%(a-1)==0:
            a-=1
            return False
        else:
            return True


#2

switch them sides

should be

a=x


#3

Move return True eight spaces to the left, and drop the else.


#4

Why a for loop is used in the excercise is because for loops are used for exact or known iterations, duplicating this line,

for n in range (2,x):

Analyze this line and see how you would alter the code to use a while loop. Because this for loop is the main component.


#5

I am now getting an error telling me that my function gives False for is_prime(2).

def is_prime(x):
    a=x
    if a==0 or a==1:
        return False
    else:    
        while a>=2:
            if x%(a-1)==0:
                a-=1
                return False
           
            return True

#6

Still too much indentation on the last line. It should be just four spaces from the left edge.

Consider also that since the opeing if statement returns a value, there is no need for an else. Just go directly to writing the while loop.

def ___:
    if ___:
        return False
    while ___:
        if ___:
            return False
    return True

#7

When I indent my last line like this I am getting an infinite loop. Not really sure what is going on here.


#8

Will need to see your code, again. Please format it. The instructions are in the Help category under Q & A Forum subcategory.


#9

def is_prime(x):
    a=x  
    if a==0 or a==1:
        return False   
    while a>1:
        if x%(a-1)==0:
            return False  
        a-=1       
    return True

I adjusted my code and I no longer get the infinite loop. However the function still fails for x=2. I think the problem is a-=1 is in the wrong place but I have tried moving it around and nothing works.


#10

Up to now we have only centered upon indentation of lines. Scope is everything in Python. Indentation defines it. We cannot be frivolous with indentation like we can in most languages. Here it means business.

That out of the way, what does this line give when x is 2, given the while condition,

while a>1:
    if x%(a-1)==0:

Answer: 0

2 is a special case in that it is the only Prime with even parity.


#11

if a < 2: return False
elif a > 2:
    while ...:
        return False
return True

#12

Thank you I finally have the code working.


#13

We can cut the need for evaluation in half by adding to the conditional...

if x < 2 or x % 2 == 0 and x != 2: return False
elif x > 2:
    while ...:
        return False
return True

#14

Since any number larger than the square root of x can only be multiplied by a number that is smaller than the square root of x, we can conclude that the factor list we need to test is nothing greater than that square root.

a = 3
while a <= x ** 0.5:
    if x % a == 0:
        return False
    a += 1

#15

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