steve123
steve123

Reputation: 51

Why does formatting cause print() to fail?

I create a 2-d array named P1Puts[][]. Everything I want to do with it works fine, UNTIL it comes time to format the output. Can anyone tell me why my attempt to format the output causes the error "tuple index out of range"? I created the example below from the shell, using live data. Note that "Trial 1 of 2" works okay, but "Trial 2 of 2" yields an error.

#=================================================
# TRIAL 1 OF 2:

>>> 
>>> for r in range(9):
    print('r',r,P1Puts[r][2])


r 0 51.73
r 1 51.8
r 2 60.46
r 3 34.62
r 4 49.69
r 5 87.93
r 6 33.37
r 7 42.59
r 8 54.6
>>> 
#=================================================
# TRIAL 2 OF 2: 
# (Note that all I did below was add formatting
#  to the third argument in print()

>>> for r in range(9):
    print('r',r,"{6.2f}".format(P1Puts[r][2]))


Traceback (most recent call last):
  File "<pyshell#21>", line 2, in <module>
    print('r',r,"{6.2f}".format(P1Puts[r][2]))
IndexError: tuple index out of range
>>> 

Upvotes: 2

Views: 61

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

You are missing a :

for r in range(9):
    print('r',r,"{:6.2f}".format(P1Puts[r][2])) # : ← missing 

Upvotes: 4

Related Questions