Indentation question


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096#

Oops, try again. Your function fails on is_prime(3). It returns None when it should return True.

I found out what was creating the error but I dont understand why. My very last // else: return True // was indented too far. But I cant wrap my head around why it isn't fine where it is.


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


#2

If on the first iteration, you either exit the function, or otherwise exit the function, then you will be exiting the function on the first iteration. There will be no second iteration, was there meant to be? If not, there shouldn't be a for-statement there at all.

If you de-indent it then you have a for-else. If you don't know what that does, then you have two reasonable options.
1) stop using it and use something you know what it does
2) learn what it does before doing anything else


#3

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