Why doesn't this code work if x = 9?


#1

Why doesn't this code work if x = 9??

def is_prime(x):
    if x < 2:
          return False
    elif x == 2:
         return True
    else:
         for n in range(2, x):
              if x % n == 0:
                   return False
               elif x % n != 0:
                   return True

6. is_prime
#2

@17navasaw,

Please re-edit your Post

  • leave one blank-line above of your code
  • select your code in the Post
  • then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.

With Courtesy to stetim94
https://discuss.codecademy.com/t/im-having-a-problem-on-lesson-11-23/2307/3?u=leonhard.wettengmx.n

or even better use
= https://discuss.codecademy.com/t/how-do-i-format-code-in-my-posts/28351
[extra's]
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet


#3

your for loop will only once, because either your if statement will evaluate to true and run, or your elif evaluate to true, and run. Either way, in the first run of the for loop, a return keyword is reached, which means the function (which includes the loop) ends.

You might want to put the return True outside of the for loop