Prime numbers with a while and a function


#1

Hey!
I am doing the Pythonic Algorithms course of codeacademy: https://www.codecademy.com/es/courses/python-intermediate-en-NYXmc/0/1

I have done a check of prime numbers with a while:

num = int(raw_input("Dame u numero \n"))
creo_que_es_primo = True
divisor = 2
while divisor < num:
	if num % divisor == 0:
		creo_que_es_primo = False
		break
	divisor += 1

if creo_que_es_primo == True:
	print "El nomero,", num, "Es  primo"
	print divisor
else:
	print "El numero", num, "No es primo"
	print divisor

But now i want to do this with a function def,:

creo_primo = True
divisor = 2
    
def isPrime(n):
	while divisor < n:
	    if (n % divisor) == 0:
            creo_primo = False
            break
       divisor += 1
       return creo_primo
            

	
# Tests....
print isPrime(3) # Should output True
print isPrime(143) # Should output False
print isPrime(790003) # Should output True

And i got this error:

But if i have this in the correct block!!


#2

the indention seems to be poorly done by your editor, i re-indented the code:

def isPrime(n):
    creo_primo = True
    divisor = 2
    while divisor < n:
        if n % divisor == 0:
            creo_primo = False
            break
        divisor += 1
    return creo_primo



# Tests....
print isPrime(3) # Should output True
print isPrime(143) # Should output False
print isPrime(790003) # Should output True

and now everything is fine


#3

Thanks very much!!
I have done this post because i didnt know exactly what was the problem, altough i have tried to use the "Un - comment region" of my editor for solve this...


#4

i have no idea what caused it, i have set my vim (a command line text-editor) to work wonderfully well for python, so i used vim to re-indent the code, and then everything was good


#5

Aww I use Komodo IDE OSX, and i am very happy with this, so i will forget this mistake...
likewise thanks!!