# Simple countdown programs to print “3, 2, 1, Action!” using while loop

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

• Are you saying it has to be a while loop? – dangee1705 Oct 3 '17 at 15:28

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):

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

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

• 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.) – Ilmari Karonen Oct 2 '17 at 10:40

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!")


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)

• 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. – Mathias Ettinger Feb 19 at 8:17

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.
• 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. – AlexV yesterday
• 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 – Sᴀᴍ Onᴇᴌᴀ yesterday