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