I have just started learning how to program using Python, and have been going through some exercises to help me improve. For one of the exercises, I have to program a Sieve of Eratosthenes, and as part of doing this, I wanted to generate a list of numbers 1 through n.
My code is as follows:
def primelist(n): #returns a list of all primes lower than or equal to n
while k in range (1, n+1):
I don't know why it's backwards, but your code is not the way to add elements to a list. Also you don't use the variable
n in your code (probably a typo), so I will just add the function to generate a list through 1 to
def primelist(n): grid= for k in range(1,n+1): grid.append(k) return grid
This code will return
[1,2,3,4,5,6,7,8,9,10] as you requested.