类型错误:“dict"对象不可调用 [英] TypeError: 'dict' object is not callable

查看:39
本文介绍了类型错误:“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

推荐答案

访问给定键的 dict 的语法是 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.

这篇关于类型错误:“dict"对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆