1
a = int(input("Enter a numeber: "))

for i in range(a):
    x = a - i
    print(x)

How can I make this count down to zero instead of 1?

martineau
  • 119,623
  • 25
  • 170
  • 301

3 Answers3

5

You could use the three-argument form of range:

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

It will start with a and then (because the step [third argument] is -1) count down to 0 because the stop value (-1 [second argument]) is excluded.

MSeifert
  • 145,886
  • 38
  • 333
  • 352
0

Just add 1 to your range.

Your program now looks like:

a = int(input("Enter a number: "))


for i in range(a+1):
    x = a - i
    print(x)

This is because range(n) goes from 0 to n-1, and so as you have it, your final iteration runs a - (a-1) = 1. To make your range go until a ( to calculate a - a ) you need to use range(a+1).

rp.beltran
  • 2,764
  • 3
  • 21
  • 29
0

You can also use a while-loop like:

a = int(input("Enter a numeber: "))
while a >= 0:
    print(a)
    a -= 1

output:

Enter a numeber: 10
10
9
8
7
6
5
4
3
2
1
0

if you want to use a for loop you just need to use a+1 in the range:

a = int(input("Enter a numeber: "))
for i in range(a+1):
    print(a-i)
Mohd
  • 5,523
  • 7
  • 19
  • 30