-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
  • 379,657
  • 97
  • 704
  • 746
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
  • 812
  • 5
  • 13
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
  • 8,545
  • 8
  • 39
  • 68
0

Alternative one line answer:

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

kam
  • 408
  • 4
  • 9