Reputation: 14062
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 output:
script executed in 0.12 seconds
Upvotes: 5
Views: 9632
Reputation: 117856
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, which is why it gave the "tuple index out of range" error.
Upvotes: 2
Reputation:
You created two format fields:
print('script executed in {time}{1:.2f} seconds'.format(time=elapsed_time))
# ^1 ^2
but only gave one argument to str.format
:
print('script executed in {time}{1:.2f} seconds'.format(time=elapsed_time))
# ^1
You need to have the number of format fields match the number of arguments:
print('script executed in {time:.2f} seconds'.format(time=elapsed_time))
Upvotes: 10