range countdown to zero

Pashta picture Pashta · Mar 28, 2018 · Viewed 9.3k times · Source

I am taking a beginner Python class and the instructor has asked us to countdown to zero without using recursion. I am trying to use a for loop and range to do so, but he says we must include the zero.

I searched on the internet and on this website extensively but cannot find the answer to my question. Is there a way I can get range to count down and include the zero at the end when it prints?

Edit:

def countDown2(start):
#Add your code here!
    for i in range(start, 0, -1):
        print(i)

Answer

user3483203 picture user3483203 · Mar 28, 2018

The range() function in Python has 3 parameters: range([start], stop[, step]). If you want to count down instead of up, you can set the step to a negative number:

for i in range(5, -1, -1):
    print(i)

Output:

5
4
3
2
1
0