D
defn noob
isPrime works when just calling a nbr but not when iterating on a
list, why? adding x=1 makes it work though but why do I have to add
it?
Is there a cleaner way to do it?
def isPrime(nbr):
for x in range(2, nbr + 1):
if nbr % x == 0:
break
if x == nbr:
return True
else:
return False
Traceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
[isPrime(y) for y in range(11)]
File "C:\Python25\Progs\blandat\myMath.py", line 9, in isPrime
if x == nbr:
UnboundLocalError: local variable 'x' referenced before assignment
Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
map(isPrime, range(100))
File "C:\Python25\Progs\blandat\myMath.py", line 9, in isPrime
if x == nbr:
UnboundLocalError: local variable 'x' referenced before assignmentTrue
adding x=1 makes it work though:
def isPrime(nbr):
x=1
for x in range(2, nbr + 1):
if nbr % x == 0:
break
if x == nbr:
return True
else:
return False
False]
list, why? adding x=1 makes it work though but why do I have to add
it?
Is there a cleaner way to do it?
def isPrime(nbr):
for x in range(2, nbr + 1):
if nbr % x == 0:
break
if x == nbr:
return True
else:
return False
[isPrime(y) for y in range(11)]
Traceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
[isPrime(y) for y in range(11)]
File "C:\Python25\Progs\blandat\myMath.py", line 9, in isPrime
if x == nbr:
UnboundLocalError: local variable 'x' referenced before assignment
Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
map(isPrime, range(100))
File "C:\Python25\Progs\blandat\myMath.py", line 9, in isPrime
if x == nbr:
UnboundLocalError: local variable 'x' referenced before assignmentTrue
adding x=1 makes it work though:
def isPrime(nbr):
x=1
for x in range(2, nbr + 1):
if nbr % x == 0:
break
if x == nbr:
return True
else:
return False
[False, True, True, True, False, True, False, True, False, False,[isPrime(y) for y in range(11)]
False]