本文介绍了TypeError:"dict"对象不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图遍历输入字符串的元素,并从字典中获取它们.我在做什么错了?
I'm trying to loop over elements of an input string, and get them from a dictionary. What am I doing wrong?
number_map = { 1: -3, 2: -2, 3: -1, 4: 1, 5: 2, 6: 3 }
input_str = raw_input("Enter something: ")
strikes = [number_map(int(x)) for x in input_str.split()]
strikes = [number_map(int(x)) for x in input_str.split()]
TypeError: 'dict' object is not callable
推荐答案
在给定键的情况下访问字典的语法为number_map[int(x)]
. number_map(int(x))
实际上是一个函数调用,但是由于number_map
不是可调用的,因此会引发异常.
The syntax for accessing a dict given a key is number_map[int(x)]
. number_map(int(x))
would actually be a function call but since number_map
is not a callable, an exception is raised.
这篇关于TypeError:"dict"对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!