In the below code, I wrote this code to find the set of prime numbers in a specific range.
I am getting output as:
local variable 'x' referenced before assignment
(note: I am using Jupiter notebook). I am getting the same error lot of times.
def prime_list(*args):
limit=args[0]
primelist=[]
for y in range (0,limit):
factor=0
for x in range(1,y+1):
if(y%x)==0:
#print("entered in to modulo loop")
factor=factor+1
if(factor<=2):
#print("{} is prime number".format(y))
primelist.append(x)
return primelist
prime_list(10)
You should insert y
in the primelist
:
def prime_list(*args):
limit=args[0]
primelist=[]
for y in range (0,limit):
factor=0
for x in range(1,y+1):
if(y%x)==0:
#print("entered in to modulo loop")
factor=factor+1
if(factor<=2):
#print("{} is prime number".format(y))
primelist.append(y)
return primelist
prime_list(10)
Output:
[0, 1, 2, 3, 5, 7]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.