python icon indicating copy to clipboard operation
python copied to clipboard

Task 1 of Meltdown Mitigation is misleading

Open punkstarman opened this issue 5 years ago • 7 comments

One of the most common blunders a novice can make is to use if-elif-else blocks instead of using a single return statement.

For instance

if a:
    return True
elif b:
    return True
else:
    return False

should simply be written

return a or b

The introduction sets up using if:elif:else: blocks. I fear that a novice will blindly fall into the trap of solving task 1 using an if.

def is_criticality_balanced(temperature, neutrons_emitted):
    if temperature < 800 and neutrons_emitted > 500 and temperature * neutrons_emitted < 500_000:
        return True
    else:
        return False

or something even worse, when it should be solved with a single return statement.

def is_criticality_balanced(temperature, neutrons_emitted):
    return temperature < 800 and neutrons_emitted > 500 and temperature * neutrons_emitted < 500_000

punkstarman avatar Sep 03 '21 12:09 punkstarman

@punkstarman 👋🏽 Thanks for logging this.

Concept Exercises are quite different from the "regular" practice exercises that were carried over from V2. They're intended to teach a specific concept, and must only use the concepts introduced as part of their dependency tree. Since this concept exercise is designed to teach conditionals, we can't (even though bools is covered first) have the exemplar.py showing the return of a bool, and not showing if, elif, and else in action.

I am happy to have someone propose revisions / additions to this exercise provided that it thoroughly covers the use of if, elif, else and only use the techniques taught in basics (Guidos Gorgeous Lasagna) and bools (Ghost Gobble Arcade Game). But I do think it's (with the changes below) decent as it stands.

Having said that - agree that multiple returns here is not ideal -- especially when it pisses off PyLint. To that end, I've re-written the exemplar.py file in PR #2556 to use a variable for return instead.

BethanyG avatar Sep 06 '21 17:09 BethanyG

I've merged #2556. Please let me know if you have further questions/issues/comments. Thanks!

BethanyG avatar Sep 06 '21 20:09 BethanyG

Having said that - agree that multiple returns here is not ideal -- especially when it pisses off PyLint. To that end, I've re-written the exemplar.py file in PR #2556 to use a variable for return instead.

PyLint is wrong in this instance. Multiple return statements is the right thing to do.

I've merged #2556. IMO, this has made things worse. I will suggest something different.

punkstarman avatar Sep 07 '21 14:09 punkstarman

Suggest away! PRs on this welcome, with the caveats listed above. 😄 I will hold this issue open, and flag it as a discussion.

BethanyG avatar Sep 07 '21 20:09 BethanyG

if a:
    return True
elif b:
    return True
else:
    return False

return a or b

I agree with this as long as the items are bools.
What if a is an integer, or list, or something that has an implicit boolean meaning? 5 or False returns 5, not True, but the above mentioned if structure does return True. While one can achieve the same with bool(5) or False, it might be a bit confusing for a beginner.

I would not simply discard the good old if. But yes, there is definitely space for a lesson with an advanced boolean handling (including the short-circuits, implicit meanings, one line returns etc).

kbuc avatar Sep 09 '21 08:09 kbuc

I agree with this as long as the items are bools.

Absolutely

I would not simply discard the good old if.

I do not mean to discard it. I mean to make proper use of it in context.

punkstarman avatar Sep 09 '21 09:09 punkstarman

I am going to close this issue. The tasks in this exercise are being reviewed/revised, but we are unlikely to change Task 1 to not use an if. We may revisit this in the future, but we need to keep this exercise focused on the use of conditionals (not Booleans) for now.

BethanyG avatar Sep 17 '21 16:09 BethanyG