'IndexError: tuple index out of range' whith str.format
What am I doing wrong?
print('script executed in {time}{1:.2f} seconds'.format(time=elapsed_time))
I get:
IndexError: tuple index out of range
Expected Result:
script executed in 0.12 seconds
+3
Boosted_d16
source
to share
2 answers
You have created two format fields:
print('script executed in {time}{1:.2f} seconds'.format(time=elapsed_time))
# ^1 ^2
but gave only one argument str.format
:
print('script executed in {time}{1:.2f} seconds'.format(time=elapsed_time))
# ^1
You need the number of format fields to match the number of arguments:
print('script executed in {time:.2f} seconds'.format(time=elapsed_time))
+7
iCodez
source
to share
You can just do
>>> 'script executed in {:.2f} seconds'.format(elapsed_time)
'script executed in 0.12 seconds'
In your original code, you have two fields {}
, but only gave one argument, so it gave the error "tuple index out the range".
+1
CoryKramer
source
to share