Is Prime, am lost


#1



Could you give me a hand?
Im lost


x = 3

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


Is_Prime(9) doesn't show the correct result
#2

A function ends the moment a return keyword is reached, this is currently happening in the first iteration of your loop

You only want to return True after the loop has finished running


#3

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

Did you mean True must be the last outcome possible? If so, this isn't working either


#4

yes, what you did is a good start. Now look:

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

look how many times the loop executes, and then read my first reply again


#5

weird... print n returns 2....

if x =9

print range(2,x) returns [2, 3, 4, 5, 6, 7, 8]

idk, what im doing wrong


#6

^ does that start to make more sense?

as you experienced, the loop ends in the first iteration, because only 2 is printed, what does this tell you?


#7

but my function says For every N in range(X).

but he stops at the first one, thus not doing for every. How is that possible?

Is there a way to write an IF function without a return?


#10

The last else part corrupt your code.


#11

i was about to write a detailed explanation about why. May i please? Thank you


#12

a function ends the moment a return keyword is reached. Even while doing a loop (the loop just breaks)

if x is not divisible by any number between 2 and x (2 included, x excluded), the number is a prime number. Then you want to return True. So after the loop has finished running.

If x is divisible by a number between 2 and x return False is fine, since the number is not a prime number, it even stops the loop, so the rest of the loop doesn't have to execute (rather efficient)

So what you need to change is that True is returned after the loop. Currently True is returned in the first iteration of the loop if x / 2 doesn't have a remainder of zero


#13

My brain understood what you said, but I had no knowledge on how to do it. So I did the most complicated solution possible. I'd appreciate if you show me how I could have corrected the earlier solution....

What i did was this:

x = 5

def is_prime(x):
    my_list = []
    if x < 2:
        return False
    if x == 2:
        return True
    else:
        for n in range(2,x):
            my_list.append(x % n)
        if 0 in my_list:
            return False
        else:
            return True
                
print is_prime(x)

This works. But I know it is overly complicated... How would I do this

in this code?


#14

that is complicated code.

Getting back to your earlier code, python relies on indention to determine what is nested inside a function/for loop. So the key is in the indention to reach else/return True at the right moment.


#15

i get an error message for any other location of the else argument


#16

really? Can i see what you tried?


#17

Here is what I've done

def is_prime(x):
    
    if x < 2:
        return False
    elif x == 2:
        return True
    else:
        
        for n in range(2, x):  
            if x % n == 0:
                return False
                break
        else:                  #asked to explain
            return True
print is_prime(int(raw_input("put some number: ")))

I can't explain it. I coppied it(( If smo could explain us, I also would be thankful))


#18

wow, i tried it and it works. No need for the break though. It works well without it!

Thanks


#19

because return will break the loop. I was so hoping you would discover it yourself, i thought that this is more valuable in my opinion, or i am wrong? Did you figure out where you went wrong?

@mardone, this topic is full of explanations? I can't tell if you read any of them without telling me.


#20

The thing that tripped me up the most was thinking that the if and else statements had to be on the same line of indentation. Can you explain how they go on different indentation lines and the code still runs? I have read thoroughly through your comments on this thread but I did not realize they could be on separate lines. After typing mardone's code it works!


#21

If you want the else to happen when the if condition evaluates to false, then they should indeed be the same indent level. This is not what you want in this exercise

What you did by placing else on the same indent level you created a for/else instead of a if/else

How does for/else work? if the condition of the loops becomes false, else will run.

If a number is not a prime, false will be returned, the loop will break, else will not run (else of doesn't run when the loop breaks)


#22

Great explanation. Thank you.