Help me here!


#1

def is_prime(x):
__n = 2
__while n<x:
____if x%n != 0:
______continue
____elif x%n == 0:
______return False
______break
____n = n+1
__if x%n != 0 and n<x:
____return True

print is_prime(5)

why is the code not showing any output? I have tried its C version, but got the same result. The program doesn’t end as if it fell into an infinite loop.


#2

That’ll print None, you have a path in your function which makes it go past its last line -> returns None (no explicit return value)


#3

what should i do if you could please direct me! I also erased the last if logic and turned into a while-else compound statement!


#4

You could add prints in your code which detail what’s being done step by step to find out where it goes awry

(You might start somewhere in the middle or somewhere you suspect could be the cause, as opposed to starting out with a complete log of actions, English highly recommended so that it makes sense when read and isn’t just a mess)


#5

Also note that you never break from the loop, so loop-else is pointless (yes, you have a break. you don’t execute it. it’s not reachable)

You can remove the continue as well if you simply do nothing else after testing divisibility. (cleaner)

if-divisor-found -> result found
reached past the loop -> no divisor -> result found


#6

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