Skip to content

Task 1 of Meltdown Mitigation is misleading #2535

Closed

Description

@punkstarman

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

Activity

  1. BethanyG commented on Sep 6, 2021

    @BethanyG
    Member

    @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.

  2. BethanyG commented on Sep 6, 2021

    @BethanyG
    Member

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

  3. punkstarman commented on Sep 7, 2021

    @punkstarman
    Author

    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.

  4. BethanyG commented on Sep 7, 2021

    @BethanyG
    Member

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

  5. kbuc commented on Sep 9, 2021

    @kbuc
    Contributor
    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).

  6. punkstarman commented on Sep 9, 2021

    @punkstarman
    Author

    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.

  7. BethanyG commented on Sep 17, 2021

    @BethanyG
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions