繁体   English   中英

类型错误:元组索引必须是整数或切片,而不是 dict

[英]TypeError: tuple indices must be integers or slices, not dict

我正在使用税率字典创建税收计算程序。 但是,每当我尝试打印字典键或值时,我都会收到多个错误。 我只想得到“键”或“值”,但我不断收到这些元组错误。

错误:

类型错误:元组索引必须是整数或切片,而不是 dict”

或者

“AttributeError: 'tuple' 对象没有属性 'items'”

或者

AttributeError: 'tuple' 对象没有属性 'value'

或者

AttributeError: 'tuple' 对象没有属性 'keys'

我什至不能打印字典。 我提供了下面的所有场景,它们不适用于这个非常简单的任务。 我不确定为什么我不断收到这些错误。

tax_rates = {
  'AB' : .05,
  'BC' : .12,
  'MN' : .13,
  'NB' : .15,
  'NL' : .15,
  'NT' : .05,
  'NS' : .15,
  'ON' : .13,
  'PE' : .15,
  'QC' : .1475,
  'ST' : .11,
  'YK' : .05
},

for key in tax_rates:
  print(tax_rates[key])

for key in tax_rates.items():
  print(key)

for value in tax_rates.items():
  print(value)

for key,value in tax_rates.items():
  print(key,value)

tax = tax_rates.keys()
print(tax)

字典后面的逗号是错误。 去掉逗号和

for key in tax_rates.items(): print(key)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM