Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Looping a Program
#7
Since I keep getting notifications about this thread, I feel inclined to comment. Neither of you are necessarily wrong; you have two different design philosophies. Between the two of you though, I find volcano's perspective more appealing (though delivered without tact).

Else clauses that don't provide instructions, such as "else: pass" are devoid of value. Imagine that someone provides the following bit of advice: "If it is raining, bring an umbrella." Do you need to also hear: "Otherwise, do whatever you'd like, the presence of an umbrella is not significant because it is not raining"? The condition for rain insinuates an exception to the rule that you do not need an umbrella. Essentially, if it is not raining, you can ignore the conditional altogether and move on.

Using the continue keyword at the end of a loop is also devoid of value. When working inside a loop, interpreter returns to the beginning of the loop once the body has been completed. Because the loop must be explicitly declared at its start, the inherent behaviors of it are arguably explicit as well (however, you could argue that those behaviors are an implicit rule of the language which is why "Explicit is better than implicit" is in the Zen of Python). Therefore, the following pattern has no usefulness:

while True:
    ...code...
    if True:
        continue
Conversely, the following pattern does have value because it alters the expected behavior of the code by terminating the loop:

while True:
    ...code...
    if True:
        break
The continue keyword only has value when it appears prior to the end of the loop body to skip the remainder of the body and start the next iteration. As a result, this conditional at the end of a loop

if repeat == 'y':
    continue
else:
    break
offers little to the code while saying too much. Because the instruction when the conditional is true is to continue the loop, I don't care about the conditional being true. Continuing the loop would have happened by default because of my explicitly declared loop. I only care about the instruction that contravenes the known behavior of the loop, which only happens when anything other than "y" has been inputted. As such, this version simplifies the conditional and tells me what I care to know without stating the obvious:

if repeat != 'y':
    break
Reply


Messages In This Thread
[split] Looping a Program - by volcano63 - Oct-05-2018, 09:26 PM
RE: [split] Looping a Program - by micseydel - Oct-09-2018, 12:26 AM
RE: Looping a Program - by perfringo - Oct-06-2018, 05:12 AM
RE: Looping a Program - by volcano63 - Oct-06-2018, 08:31 AM
RE: Looping a Program - by perfringo - Oct-07-2018, 09:00 AM
RE: Looping a Program - by volcano63 - Oct-07-2018, 09:26 AM
RE: Looping a Program - by perfringo - Oct-08-2018, 11:31 AM
RE: Looping a Program - by stullis - Oct-08-2018, 02:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Results of this program in an excel file eisamabodian 1 1,612 Feb-11-2022, 03:18 PM
Last Post: snippsat
  Need help looping this program JakobeTheKid 1 2,128 May-19-2019, 05:30 AM
Last Post: SheeppOSU
  Looping a Program DavidRobinsons 4 3,591 Oct-09-2018, 12:14 AM
Last Post: micseydel
  [split] Coin Flip Program Crackity 5 4,895 Sep-25-2017, 03:48 AM
Last Post: Crackity

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020