Python 元组索引超出范围意味着什么?

Python 元组索引超出范围意味着什么?,python,printing,format,Python,Printing,Format,谁能解释一下原因: print('{0} complete (down: {1} kb/s up: {2} kb/s {3} peers: {4} {5}'.format('state.progress * 100', 'state.download_rate / 1000', 'state.upload_rate / 1000', 'state.num_peers', 'state_str[state.state]')) 导致元组索引超出范围?您没有“第五个”(或者更确切地说,索引5)元素,

谁能解释一下原因:

print('{0} complete (down: {1} kb/s up: {2} kb/s {3} peers: {4} {5}'.format('state.progress * 100', 'state.download_rate / 1000', 'state.upload_rate / 1000', 'state.num_peers', 'state_str[state.state]'))
导致元组索引超出范围?

您没有“第五个”(或者更确切地说,索引5)元素,因为第一个元素的索引为0

因此,必须有6个元素才能使用索引5

print('{0} complete (down: {1} kb/s up: {2} kb/s {3} peers: {4} {5}'.format('state.progress * 100', 'state.download_rate / 1000', 'state.upload_rate / 1000', 'state.num_peers', 'state_str[state.state]'))
在这里,您的要素是:

0: 'state.progress * 100'
1: 'state.download_rate / 1000'
2: 'state.upload_rate / 1000'
3: 'state.num_peers'
4: 'state_str[state.state]'
5: nil
您没有“第五个”(或者更确切地说,索引5)元素,因为第一个元素的索引为0

因此,必须有6个元素才能使用索引5

print('{0} complete (down: {1} kb/s up: {2} kb/s {3} peers: {4} {5}'.format('state.progress * 100', 'state.download_rate / 1000', 'state.upload_rate / 1000', 'state.num_peers', 'state_str[state.state]'))
在这里,您的要素是:

0: 'state.progress * 100'
1: 'state.download_rate / 1000'
2: 'state.upload_rate / 1000'
3: 'state.num_peers'
4: 'state_str[state.state]'
5: nil

您没有索引5元素。您调用了{5},因为您只给出了5个元素。所以元组索引超出范围在可能更通俗的单词中,没有索引为5的元素。从技术上讲,索引4是“第五个”元素,除非您计算“第零个”元素。鉴于您已正确地将
0
用于第一个元素,您能否解释一下为什么您还需要其他内容?另外,请注意,执行“{0}”。format('state.progress*100')将返回'state.progress*100'[即文字字符串]注意state.progress*100的值,您没有索引5元素。您调用了{5},因为您只给出了5个元素。所以元组索引超出范围在可能更通俗的单词中,没有索引为5的元素。从技术上讲,索引4是“第五个”元素,除非您计算“第零个”元素。鉴于您已正确地将
0
用于第一个元素,您能否解释一下为什么您还需要其他内容?另外,请注意,执行“{0}”。format('state.progress*100')将返回'state.progress*100'[即文字字符串]请注意state.progress*100I将是世界上最伟大的python程序员。一旦我学会了数数。你能解释一下我是如何强制打印在python3中显示为两个小数点的吗?a)这是一个新问题b)通过搜索很容易找到我将成为世界上最伟大的python程序员。一旦我学会了数数。你能解释一下我是如何在python3中强制打印到小数点后两位的吗?a)这是一个新问题b)可以通过搜索轻松找到