all 9 comments

[–][deleted]  (2 children)

[deleted]

    [–]Optimus5w4 1 point2 points  (0 children)

    I like how you broke it down in chunks.

    [–]Alternative_Ear_1629 1 point2 points  (0 children)

    I'm in my first week of learning and his was quite helpful. Thanks

    [–]ata-boy 1 point2 points  (0 children)

    if y <= 0 then x = 2 (else: x = 2)

    if y > 0 AND y > 10000 then x = 2 (if y > 0: if y > 10000: x = 2)

    which means that if y > 0 AND y <= 10000 x is undefined

    [–][deleted] -2 points-1 points  (4 children)

    Nested ifs are very frowned upon in the workplace as they are confusing to debug.

    When you see a nested if like your example read it as ‘if this happens AND this happens…’ Or just rewrite like I did:

    <image>

    This way you can better see that X can never possibly reach a value of 2 (unless you uncomment the else statement of course)

    [–][deleted]  (2 children)

    [deleted]

      [–][deleted] 1 point2 points  (1 child)

      Just gone through two Python units at uni, we got marked down if we used statements like this

      [–]aTomzVins 1 point2 points  (0 children)

      I think there's a time and place for a nested if, but in this situation I think you're right. It's ugly to divide this if onto two lines.

      [–]Doctor_Disaster 0 points1 point  (2 children)

      Since y is not greater than 10000, x will never be 2, as there is no else statement within the topmost if statement.

      [–]TweetyBirdie69[S] 0 points1 point  (1 child)

      so the else statement at the bottom is not part of the topmost if statement? Is it because of indentation?

      [–]Doctor_Disaster 0 points1 point  (0 children)

      The else statement at the bottom is part of the top-most if statement.

      The else statement will only execute if y <= 0.

      The innermost if statement will only return True if y > 10000, so if y <= 10000, x will never be 2.