#6 Prime ][ Indentation Issue ][


#1


#6 Prime

Issue with indention of the else statement within the for loop
Changed the statement to align with the loop instead of with the if condition
Solution works

Thoughts on how the indention of the else statement changed the solution


def is_prime(x):
     if x > 1:
          for n in range(2, x - 1):
               if x % n == 0:
                    return False
          else: 
               return True
     else: 
          return False


#2

A function ends the moment a return keyword is reached

Because you had an if/else clause first, in the first iteration of the loop a return was reached. (either if or else)

change if/else to for/else will mean that if a number is a prime, true is returned after the for loop has finished running


#3

Thanks for that explanation

The for/else perspective helped with understanding
how the function ended with the return keyword


#4

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