Home > Blockchain >  Keep getting "TypeError: 'dict' object is not callable"
Keep getting "TypeError: 'dict' object is not callable"

Time:03-16

Keep getting "TypeError: 'dict' object is not callable" when trying to access the letter_pair value

def get_value(letter_pair):
  # Create a dictionary given two lists
  # see https://towardsdatascience.com/15-things-you-should-know-about-dictionaries-in-python-44c55e75405c
  # 2. Create a dictionary with dict() constructor
  letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
  values = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]
  letters_values = dict(zip(letters, values))
  # {'a':1, 'b':2,...}
  
  if letter_pair[0] in letters_values and letter_pair[1] in letters_values:
    if letters_values('letter_pair[0]') > letter_values('letter_pair[1]'): # <-- this is the line where it happens
      return -1
    elif letter_values('letter_pair[0]') == letter_values('letter_pair[1]'):
      return 0
    else:
      return 1
  else:
    return 2

get_value('co')

CodePudding user response:

the parentheses are used for calling functions/classes use brackets instead so change

letters_values('letter_pair[0]')

to

letters_values['letter_pair[0]']

also in letters_values you have letters_pair[0] in quotations, which you need to remove and make it

letters_values[letter_pair[0]]

then do that for every other dictionary object you have.

  • Related