Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'dict' object is not callable

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 
like image 212
Enchanterkeiby Avatar asked Jul 09 '11 12:07

Enchanterkeiby


2 Answers

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.

like image 195
jena Avatar answered Sep 24 '22 01:09

jena


Access the dictionary with square brackets.

strikes = [number_map[int(x)] for x in input_str.split()] 
like image 38
W. Kevin Hazzard Avatar answered Sep 21 '22 01:09

W. Kevin Hazzard