Closed. This question is
off-topic。它当前不接受答案。
想改善这个问题吗?
Update the question,所以它是
on-topic,用于堆栈溢出。
7个月前关闭。
当尝试打印包含一系列项目的字符串时,我不断收到错误消息:
元组索引超出范围
出现问题的功能是
sales_statement.format
。
在我要复制的示例中,“格式”的功能为绿色。我的是蓝色的。我已经尝试过多次重新键入函数,但是仍然会收到错误消息。这是代码。
sales = {'price':3.24,'no. of items':4, 'person':'Chris'}
sales_statement='{} bought {} items at a price of {} each, for a total of {}'
print(sales_statement.format(sales['person'], sales['price'], sales['no. of items']*sales['price']))
错误:
IndexError Traceback(最近一次通话)
在
----> 1个print(sales_statement.format(sales ['person'],sales ['price'],sales ['no。of items'] * sales ['price']))
IndexError:元组索引超出范围
输出应显示为
克里斯以每件3.24的价格购买了4件商品,总计12.96
我发现了错误。这是修改后的代码:
sales = {'price':3.24,
'number':4,
'person':'Chris'}
sales_statement='{} bought {} items at a price of {} each, for a total of {}'
print(sales_statement.format(sales['person'],sales['number'],sales['price'],sales['price']*sales['number']))
输出:
克里斯以每件3.24的价格购买了4件商品,总计12.96