Home > front end >  How do you label your output of a for loop line by line using numbers?
How do you label your output of a for loop line by line using numbers?

Time:03-02

sqaure = 1
start = 1443

end = start   96*3   1

for number in range(start, end, 3):

    

I want to make the output look like:

  1. 1443
  2. 1446
  3. 1449 etc.

CodePudding user response:

You could use enumerate() to get the first number in your output and then use an f-string to format the output:

for i, number in enumerate(range(start, end, 3), start=1):
    print(f"{i}. {number}")

Output:

1. 1443
2. 1446
3. 1449
4. 1452
5. 1455
6. 1458
7. 1461

CodePudding user response:

One thing you could do is set another var (let's say i), and increment it every time the loop runs.

sqaure = 1
start = 1443

end = start   96*3   1

i = 0

for number in range(start, end, 3):
    i  = 1
    print(i, number)

You first set i to 0, and every time the loop runs increment it by one, so you get the number of run.

  • Related