The function is supposed to load a list and print the sum of the first and last primes in the list. I have written it so that a new list is made and the idea i that only primes are appended to it - if its not a prime, it should go back to the beginning of that for loop. I want my function to go back to the beginning of the for loop after
if L[i] % j == 0:
as I only want prime numbers to be appended to y, but I don't think 'break' is the right thing to do here as I keep having my entire list being printed when I call the function. I've tried 'break', 'pass', 'continue' and an infinite loop (although I may not have done that correctly). Please help???
def first_plus_last_prime(L):
y = []
for i in range(len(L)):
if L[i] < 2:
break
for j in range(2, L[i]):
if L[i] % j == 0:
break
y.append(L[i])
print(y)
print(y[0])
print(y[-1])
t = y[0] + y[-1]
return t