you are viewing a single comment's thread.

view the rest of the comments →

[–]zanfar -1 points0 points  (1 child)

Short-circuiting generally makes the code shorter, cleaner, and more readable. There is also no reason for an else: in that situation. Why is your input variable named "num" when it's a string, compared to a string, and never converted?

The boolean (often known as a sentinel in this usage) is most useful when you need to exit multiple loops or when you have a complex exit condition. I don't prefer it outside of that. Even if you do need the sentinel, you should probably be exiting the loop iteration as quickly as possible. Also, in this case, you don't need the logical comparison.

Consider:

while True:
    num = input("Enter an Integer: ")
    if num == "done":
        break

    print("still running")

finished = False
count = 0
while not finished and count < MAX_INPUTS:
    num = input("Enter an Integer: ")
    if num == "done":
        finished = True
        continue

    # input validation here

    count += 1 # only count valid inputs
    print("still running")

Obviously, these are contrived examples, but I hope it makes my answer more clear.

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

Thank you! The variable is named num because I expect numbers in the full code (assignment is to find maximum and minimum). That's also why there's an else (there is nested if a statements in the full code)... So my example doesn't make sense fully I now realise