So the idea is to make a function that returns all prime numbers between any two numbers (inclusive). Below, I'll put the function I wrote correctly, and the one I wrote incorrectly.
The mistake I made with the incorrect one is with the indentation of the "else" statement. But I'm not sure why one is correct and the other is not!
I'm also curious about the output I am getting from the incorrect function, which shows mostly duplicates of prime numbers but occasionally throws in a single number that is NOT a prime number, but rather a number divisible by 3 and another prime number. I have been trying to wrap my head around how the function produced this result (so I can understand the mistake), but I'm stumped!
Any help would be much appreciated
##This is the correct version: ##########################
def primefinder(start, end):
primes = []
for number in range(start, end +1):
if number > 1:
for i in range(2, number):
if number % i == 0:
break
else:
primes.append(number)
return primes
## This is the incorrect version:##############
def primefinder(start, end):
primes = []
for number in range(start, end +1):
if number > 1:
for i in range(2, number):
if number % i == 0:
break
else: ##<--This is my mistake##
primes.append(number)
return primes