I am trying to learn Python, but I'm still quite new at it. I am attempting to create a list of numbers from 2 up to the number that the user will input and go through the list and remove all non-prime numbers from that list, then print it back out. I am having trouble calculating since I keep getting the error: list index is out of range. I was thinking of using a for loop but then the variable i would be lower than variable current and I need to make sure i is always higher than current as it goes through the list. I am only allowed to use basic functions and loops for the task.
counter = 2
current = 2
n = int( input( "Please enter a number larger than 2. " ) )
while counter <= n:
userList.append( counter )
counter = counter + 1
print( "Printing out list " )
print( userList )
i = 1
while i <= len( userList ):
if userList[ i ] % current == 0:
userList.remove( userList[i] )
i = i + 1
else:
current = current + 1
print( userList )