[英]Reciveing the Error code: TypeError: tuple indices must be integers or slices, not 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.