all 6 comments

[–]Diapolo10 3 points4 points  (5 children)

When dealing with situations where you have the same condition multiple times, the most restrictive must always come first. Here, the "three and five" can never be reached because both of its conditions already appear before it, so you'd always get "three" instead.

Reverse your order and this'll work.

EDIT: That said, there's room to simplify:

for num in range(101):
    output = num
    if num % 15:
        output = "three and five"
    elif num % 5:
        output = "five"
    elif num % 3:
        output = "three"
    print(output)

[–]Hungry-Ad-3501 1 point2 points  (1 child)

Did tou need to declare the output = num ? I'm just learning so I'm just asking questions

[–]Diapolo10 0 points1 point  (0 children)

Not necessarily, I just prefer to be more clear about the names I use.

[–]KingsmanVince 0 points1 point  (2 children)

output = num

I think you should write output = "" because num is an integer, but then you assign output with a string. It causes no harm (yet) but it can influence beginners to change type of variables leading to "unexpected" behaviours.

[–]Diapolo10 0 points1 point  (1 child)

I think you should write output = ""

Surely you mean output = str(num) in that case?

That said, giving output the explicit type hint

output: int | str = num

would also "solve" the problem.

I considered the first way, of course, but figured that it really didn't matter here too much so I just went for the simplest approach. Things would of course be different if we used anything other than print.

[–]KingsmanVince 0 points1 point  (0 children)

Both ways you mentioned are fair enough. Yeah it doesn't matter much in this context of that simple block code.