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


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

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

6. is_prime


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

or even better use
= https://discuss.codecademy.com/t/how-do-i-format-code-in-my-posts/28351


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