-1

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
cs95
  • 330,695
  • 80
  • 606
  • 657
Max
  • 21
  • 3

3 Answers3

2

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/

Adrien Logut
  • 792
  • 4
  • 12
1

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.

JacobIRR
  • 7,517
  • 7
  • 33
  • 58
0

Alternative one line answer:

[print(x) for x in range(6, 0, -1)]

kam
  • 348
  • 3
  • 9