简体   繁体   中英

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

I'm creating a tax calculation program using a dictionary of tax rates. However, whenever I try to print out the dictionary keys or values, I keep getting multiple errors. I am looking to get just the 'keys' or 'values' but I keep getting these tuple errors.

Errors:

TypeError: tuple indices must be integers or slices, not dict"

or

"AttributeError: 'tuple' object has no attribute 'items'"

or

AttributeError: 'tuple' object has no attribute 'value'

or

AttributeError: 'tuple' object has no attribute 'keys'

I can't even print the dictionary. I have provided all the scenarios below and they do not work for this very simple task. I'm not sure why I keep getting these errors.

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)

The comma after your dictionary is the error. Remove the comma and

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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