I'm trying to iterate through a list (range entered by user), at first assume that all numbers are prime (P), and have the program iterate through the list. When the element in the list is P, I want to iterate through all multiples within the range and change them to N. When the element is N, I want the program to move onto the next number (I set 0 and 1 to not prime, as they are exceptions). I'm running into an issue with indexing however, I get the error:
list1[number1] = 'N'
IndexError: list assignment index out of range
When I run the program I have. Here is the code:
# input
n = int(input("Enter a positive integer greater than or equal to 10: "))
while n < 10:
print ("Invalid! Try again")
int(input("Enter a positive integer greater than or equal to 10: "))
# create list
new_n = n + 1
list1 = ['P'] * new_n
# set non prime
list1[0] = 'N'
list1[1] = 'N'
# set up loop
counter = 0
for x in list1:
counter1 = 2
if list1[counter] == 'P':
for y in list1:
number1 = counter * counter1
list1[number1] = 'N'
counter1 += 1
counter += 1
else:
counter += 1
Any help would be appreciated! Thanks.