0

In the code below I'm trying to print the keys and values of a list aligned, but I don't know why it doesn't work as expected!

for k, v in enumerate(time):
 print(f'{k : >5}', end=' ')
  for d in v.values():
   print(f' {str(d): <17}', end='')
  print()

The result on PyCharm is always the same as below:

 ----------------------------------------
 0  Ronaldo          [3, 3, 3]         9                
 1  Joe            [3, 2]            5                
 2  Renato           [3, 4, 2, 5, 6]   20               
 ----------------------------------------*
Barmar
  • 741,623
  • 53
  • 500
  • 612
RenatoP
  • 1
  • 1
  • Have you tried checking PyCharm console's font? It may NOT be monospaced, which means everything blows up – Marco Balo Apr 22 '22 at 15:09
  • 2
    You are padding your names to a maximum of 5 characters, but some of them are longer. You can pad them a constant amount that you know to be greater than the longest name, or find the maximum length necessary in the code if you want a better solution. – Jeremy Apr 22 '22 at 15:13
  • Can you just force the column width with a formatting definition, e.g., print('%-20s' % k) – bici.sancta Apr 22 '22 at 15:35
  • I've tried to adjust the PyCharm console's font, but it didn't change the misalignment. Also made some changes to the code and, apparently, what messes the alignment is when the user has short names, for example Joe. Whenever I use his name the other parts of the code are pushed further left compared to the rest of the users. – RenatoP Apr 24 '22 at 18:43
  • # main code print(f"{'CODE '}", end='') for k in player.keys(): print(f' {k :<15}', end=' ') print() print('-' * 47) for i, v in enumerate(team): print(f' {i :>4}', end=' ') for data in v.values(): print(f' {str(data): <17}', end=' ') print() – RenatoP Apr 24 '22 at 18:46
  • # Print CODE Name Goals Total ----------------------------------------------- 0 Renato [3, 2, 4] 9 1 Binga [4, 3, 2, 5] 14 2 Gah [1, 2] 3 3 Joe [4, 5] 9 4 Breno [3, 2, 4, 1] 10 ----------------------------------------------- – RenatoP Apr 24 '22 at 18:48

0 Answers0