14
\$\begingroup\$

My program is a simple while loop making a countdown starting from 3 and shouts "Action!" upon reaching zero and then breaks. I would like to know which of the following ways is better in means of clarity and functionality. I am aware that there are other ways to achieve the same thing. I would like to hear your thoughts and suggestions.

countDown = 3
while (countDown >= 0):
    print(countDown)
    countDown = countDown - 1
    if countDown == 0:
        print("Action!")
        break

or

countDown = 3
while (countDown >= 0):
    if countDown != 0:
        print(countDown)
        countDown = countDown - 1
    else:
        print("Action!")
        break
\$\endgroup\$
  • \$\begingroup\$ Are you saying it has to be a while loop? \$\endgroup\$ – dangee1705 Oct 3 '17 at 15:28
21
\$\begingroup\$

Why does it has to be a while?

Maybe it is preference but for loops look cleaner.

Reversing can be done by using reversed function which reverses a iteration

countdown = 3
for count in reversed(range(1, countdown+1)):
    print(count)
print('action!')

Or you could use the step parameter in range(start, end, step) and rewrite the for loop to

for count in range(countdown, 0, -1):
\$\endgroup\$
  • \$\begingroup\$ Very good point, however list(range(1, 3, -1)) == []. reversed(range(1, countdown + 1)) may be easier to understand, and reason with. \$\endgroup\$ – Peilonrayz Oct 2 '17 at 9:34
  • 12
    \$\begingroup\$ IMO, range(countdown, 0, -1) looks cleaner and more idiomatic than reversed(range(1, countdown+1)). Opinions may vary, of course. \$\endgroup\$ – Ilmari Karonen Oct 2 '17 at 10:45
20
\$\begingroup\$

You can get rid of some instructions by writing this instead:

count_down = 3
while (count_down):
   print(count_down)
   count_down -=  1
print('Action!')

Note that I have replaced countDown by count_down to comply with PEP8' naming conventions.

Code explanation:

count_down -= 1 is equivalent to count_down = count_down - 1. You can read more on Python basic operators.

You do not need to check within the while loop if count_down reached the 0 value because it is already done when you coded while (countDown>=0). I mean you are duplicating the checking. In order to keep DRY, I just decrement the value of count_down by 1 and the break instruction is done by default as I am testing while(count_down) meaning if count_down != 0 in this context (because it also means while count_down is not False or None).

\$\endgroup\$
  • 12
    \$\begingroup\$ Why the parentheses in while (count_down):? And while it's somewhat a matter of taste, I'd argue that while count_down > 0: would express the implied intent better in this case. (In particular, it ensures that the loop will still terminate even if someone changes the initial value of count_down to, say, 3.5 or -1.) \$\endgroup\$ – Ilmari Karonen Oct 2 '17 at 10:40
4
\$\begingroup\$

I think your first example is better in terms of clarity, although you could replace while (countDown >= 0): with while (countDown > 0):, allowing you to remove your break statement.

Ex:

countDown = 3
while (countDown > 0):
    print(countDown)
    countDown = countDown - 1
    if countDown == 0:
        print("Action!")
\$\endgroup\$
3
\$\begingroup\$

Why not try recursion? Clean and simple.

num = 10

def countdown(num):
    if num == 0:
        print("Action!")
        return
    print(num)
    countdown(num-1)
countdown(num)
\$\endgroup\$
  • 2
    \$\begingroup\$ Because recursion is limited in Python and countdown(2000) won't reach completion? If it's simpler to use idiomatic constructs, I see no reason to bring recursion in. \$\endgroup\$ – Mathias Ettinger Feb 19 at 8:17
-1
\$\begingroup\$

count_down = 5

while count_down > 0:

print(count_down - 1)

count_down -= 1

if count_down == 0:

print ("Action!")

New contributor
mulenda daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Code Review! Your question currently has a very, very low quality, if it can be seen as answer at all. Please fix your code formatting and explain your reasoning on why you think the presented approach is better than the original code. \$\endgroup\$ – AlexV 20 hours ago
  • 1
    \$\begingroup\$ Welcome to Code Review! Going along with what AlexV said, it appears that you have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process. Please read Why are alternative solutions not welcome? - From Review \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ 20 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.