6/15


#1

this is the code i am using

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

And this is the code i get when using get code

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

isn’t “if” incomplete without an “else” . And the way it looks to me, it’ll return true every time , whether the if condition is satisfied or not.
Also the code i am using given me an error.
“Your function fails on is_prime(2). It returns None when it should return True.
Run”


#2

Hi, I can’t really see the indents for your code, could you paste your code in the form of code? (6th button in the header of the text box when you edit)


#3

hey thanks for that, i edited it


#4

why would if be incomplete without else? Who says they belong together? Its possible to only want to do something if a certain condition.

no? return True has the same indent level as the for loop, as such, its outside the for loop. It will run after the for loop made all its iterations

By default, a function returns None at the end. Its the last thing a function does. If we want to return something else at the end of the function, we can use the return keyword. Thus, the function ends when a return keyword is reached

so if x % n == 0 evaluates to true, return False is reached, ending the function


#5

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