这是网上的错误例子:
dicts = [ {'name': 'Michelangelo', 'food': 'PIZZA'}, {'name': 'Garfield', 'food': 'lasanga'}, {'name': 'Walter', 'food': 'pancakes'}, {'name': 'Galactus', 'food': 'worlds'} ] string = "Hi, I'm {name} and I love to eat {food}!" def string_factory(dicts, string): for item in range(0, len(dicts)): values = item[dicts] single_dict = string.format(**values) print(single_dict) string_factory(dicts, string)
'int' object is not subscriptable
我发现只要这样就会报错:
item=0 print(item["asdf"])
TypeError: 'int' object is not subscriptable
item=0.1 print(item["asdf"])
TypeError: 'float' object is not subscriptable
初步总结:把int型变量当dict使用,取值就会报错