6. is_prime


So I wrote this extremely long piece of code… It works, but i just feel like it should be shorter and simpler.

def is_prime(x):
n = range(2, x - 1)
if x == 2:
return True
elif x % 2 == 0:
return False
elif x == 3:
return True
elif x % 3 == 0:
return False
elif 2 > x:
return False
elif x == 5:
return True
elif x % 5 == 0:
return False
elif x == 7:
return True
elif x % 7 == 0:
return False
return True


you could use range() in a for loop?


I’m still getting used to that, I don’t fully understand it yet. Thanks for the reply.


range produces a list:

print range(5)

and lists can be looped:

for item in your_list:

but these concepts have been taught, you can always revisit earlier exercise or documentation if you need a syntax refresher. There is no shame in that.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.