k_07
k_07

Reputation: 50

How do you label your output of a for loop line by line using numbers?

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

Upvotes: 0

Views: 548

Answers (2)

aaossa
aaossa

Reputation: 3852

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

Upvotes: 2

1tai
1tai

Reputation: 380

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.

Upvotes: 0

Related Questions