Bart Bart - 1 month ago 22
Python Question

two for statements go into print a loop

I have a program to print out a scorebord or ranking list and at this moment I have a list with team name, score and played games but I also want to print out the position with

range()
.
The code goes as follows:

for a in rankinglist:
for pos in range(1, 33):
print(format(pos) +
format(a.name, '>18') +
format(str(a.games), '>7') +
format(str(a.score), '>11'))


rankinglist
is the list of teams and
pos
should be a range from 1 to 32.
My intention was to print out this:

1 team1 0 0
2 team2 0 0
3 team3 0 0
4 team4 0 0
5 team5 0 0
6 team6 0 0
7 team7 0 0
8 team8 0 0
9 team9 0 0
10 team10 0 0
...


but instead I get loop which prints out every team 32 times.

1 team1 0 0
2 team1 0 0
3 team1 0 0
4 team1 0 0
5 team1 0 0
6 team1 0 0
7 team1 0 0
8 team1 0 0
9 team1 0 0
10 team1 0 0
11 team1 0 0
12 team1 0 0
13 team1 0 0
14 team1 0 0
15 team1 0 0
16 team1 0 0
17 team1 0 0
18 team1 0 0
19 team1 0 0
20 team1 0 0
21 team1 0 0
22 team1 0 0
23 team1 0 0
24 team1 0 0
25 team1 0 0
26 team1 0 0
27 team1 0 0
28 team1 0 0
29 team1 0 0
30 team1 0 0
31 team1 0 0
32 team1 0 0


Anyone knows a way to solve this?

Answer

What about this?

for pos, a in enumerate(rankinglist, 1):
    print(format(pos) +
      format(a.name, '>18') +
      format(str(a.games), '>7') +
      format(str(a.score), '>11'))