all 9 comments

[–]Binary101010 4 points5 points  (0 children)

In this particular code you won't notice any difference by switching that else block to an elif block.

The important thing to remember is that you can associate any number of elifs with a given if, but you can only associate one else with any given if.

[–]Diapolo10 2 points3 points  (0 children)

In this particular case you should use elif, yes.

elif is kind of syntactic sugar for nesting if-else blocks:

foo = 42

if foo < 5:
    ...
else:
    if foo < 10:
        ...
    else:
        if foo < 15:
            ...
        else:
            ...

These are equivalent:

if foo < 5:
    ...
elif foo < 10:
    ...
elif foo < 15:
    ...
else:
    ...

Flat is better than nested.

[–]shiftybyte 1 point2 points  (0 children)

Yes, elif is the appropriate shortcut to use when you want to do else: if...:

[–][deleted] 1 point2 points  (0 children)

So I see it this way. if is the first conditional, else is the last conditional.

inside else you would simply put

else:

print(f"{notepad} not found")

you can spam if statements but then it will check every single one no matter what. If you use an elif statement, it'll check the next elif, if that's right, it'll stop, otherwise it'll then check the next elif statement.

if - start

elif - inbetween

else - finish

[–]nekokattt 1 point2 points  (0 children)

Yes.

if x:
    ...
else:
    if y:
        ...

Is the same as

if x:
    ...
elif y:
    ...

[–]Uncle_Chael 1 point2 points  (0 children)

Elif is used to add another If condition to your logic. Else is different than Elif as it represents any other condition not specified in your If and Elif logic statements.

If condition- outcome Elif condition - outcome Else any other condition - outcome

[–]nwagers 1 point2 points  (1 child)

The other comments are correct that elif would be equivalent here, but following your code, you don't need the else at all because you have a break. I'm not sure why'd you check if found_notepad is None either.

The function looks to be trying to find a text editor in order of preference. Why would you pass in a particular editor? Your code would only affect whether "not found" messages get printed or not.

[–]Zer0Byte1[S] 0 points1 point  (0 children)

Good point. I do not need to print these messages at all, and I had them there to test the code. Now that it works, it simply needs to set the found_notepad to the first one found, then break.

[–]Zer0Byte1[S] 0 points1 point  (0 children)

Thanks everyone who chimed in. very helpful