Why don't I have to use else?


#1

I am doing exercise 6. (is_prime), and I kept having trouble with my code so I ended up looking at theirs and it was exactly the same except for the fact that the last line was just: return true, instead of
else:
return True
why does this matter, and why don’t I need an else statement?

this was their code:

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

this was my code:

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

#2

it shouldn’t matter, both should pass. When you use for/else, using if/else will throw an error


#3

I’m confused, are you saying that neither should work because there shouldn’t be an if/else inside of a for/else?


#4

Why would you? What purpose would it serve? Every single thing that you write needs to have purpose, this looks like you just wrote something, looks like you meant for the else to be part of the if-statement and then just shuffled indentation around without any plan

The way programming goes is that you combine parts that you fully understand in order to get some desired result.
If you don’t know why something’s there, then there’s really no reason why it should do what you want either, it’s like saying random words, it won’t be shakespeare!

Or maybe you can argue for why it would be needed. But you’d have to include that when you ask about it.

You seem to be saying both that it should be needed, and that it shouldn’t matter either way too. Well, it doesn’t matter, it doesn’t do any difference, so therefore it shouldn’t be there, if you don’t have a break in your loop then your loop shouldn’t have an else-part either, because then the else-part serves no purpose.


#5

I thought that I was saying that for the rest of the time when x % n != 0, it should print True


#6

There’s no printing there. Nor should there be.

That n doesn’t divide x doesn’t tell you much, so you shouldn’t be returning at that point either (which means that you did mean if-else, not for-else)

if something:
    do stuff
else:
    do other stuff

The else part has to line up with the if, you can’t do this, this isn’t a thing:

    if something:
        do stuff
else:
    do other stuff

That else will be associated with whatever is above that if-statement:

if something:
    if something:
        do stuff
else:
    do other stuff

As soon as you start moving indents around, you should just start over. They mean something, they’re not there to be adjusted until it seems to work. When you write code you’re describing something, so every part of what you’re saying has to be under your control or you will be saying something different from what you mean, and you won’t know what you said


#7

ok I think i understand thank you!


#8

Oh and you have a TAB character in there as well. Don’t mix them (this is codecademy’s editor’s fault, it should be insterting 4 spaces, not tab characters)

I’d rewrite this:

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

To:

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

(no change in meaning whatsoever, just removing pointless else’s, removing the tab character, changing indentation to multiples of 4 (2 is ok too, but 4 is the general preference in the python community))

These don’t go into your python code. At all.

'	' <- tab character

#10

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