gpt4 book ai didi

python - 'IndexError : tuple index out of range' whith str. 格式

转载 作者:太空宇宙 更新时间:2023-11-03 12:59:28 25 4
gpt4 key购买 nike

我做错了什么?

print('script executed in {time}{1:.2f} seconds'.format(time=elapsed_time))

我得到:

IndexError: tuple index out of range

预期输出:

script executed in 0.12 seconds

最佳答案

您创建了两个格式字段:

print('script executed in {time}{1:.2f} seconds'.format(time=elapsed_time))
# ^1 ^2

但只给str.format一个参数:

print('script executed in {time}{1:.2f} seconds'.format(time=elapsed_time))
# ^1

您需要使格式字段的数量与参数的数量相匹配:

print('script executed in {time:.2f} seconds'.format(time=elapsed_time))

关于python - 'IndexError : tuple index out of range' whith str. 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27843255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com