def is_prime(num):
for i in range (2, num):
if num % i == 0:
return False
else:
return True
for i in range(1, 20):
if is_prime(i + 1):
print(i + 1, end=" ")
print()
I think it has something to do with the range function and value of 2 in the lower code?
expected output – 2 3 5 7 9 11 13 15 17 19
my output – 3 5 7 9 11 13 15 17 19
>Solution :
For is_prime(), when num = 2, the for loop is skipped entirely and the function returns None. None is a falsy value, so 2 is never printed.
Here is an is_prime() implementation that resolves this issue:
def is_prime(num):
for i in range(2, num):
if num % i == 0:
return False
return num != 1