How do you create a display of a list of variables, stemming from one number, all while using a for loop?
Here is what I am thinking of:
Hours = 6
for x in Hours():
Print(x <= Hours)
# This is obviously wrong
The answer:
6
5
4
3
2
1
Use the range
function with a step of -1
In your case: range(6,0,-1)
for i in range(6, 0, -1):
print(i)
Explication of the range function: https://www.pythoncentral.io/pythons-range-function-explained/
Use this one-liner:
print('\n'.join([str(i) for i in range(6, 0, -1)]))
This gets a list of integers from 6 to 0 (excluding zero), stepping backwards by one, takes a string of each int str(i)
and joins
these with newlines \n
.