“Function object is unsubscriptable” in basic integer to string mapping function

StackOverflow https://stackoverflow.com/questions/3033563

  •  27-09-2019
  •  | 
  •  

문제

I'm trying to write a function to return the word string of any number less than 1000.

Everytime I run my code at the interactive prompt it appears to work without issue but when I try to import wordify and run it with a test number higher than 20 it fails as "TypeError: 'function' object is unsubscriptable".

Based on the error message, it seems the issue is when it tries to index numString (for example trying to extract the number 4 out of the test case of n = 24) and the compiler thinks numString is a function instead of a string. since the first line of the function is me defining numString as a string of the variable n, I'm not really sure why that is.

Any help in getting around this error, or even just help in explaining why I'm seeing it, would be awesome.

def wordify(n):
    # Convert n to a string to parse out ones, tens and hundreds later. 
    numString = str(n)

    # N less than 20 is hard-coded.
    if n < 21:
        return numToWordMap(n)
    # N between 21 and 99 parses ones and tens then concatenates.
    elif n < 100:
        onesNum = numString[-1]
        ones = numToWordMap(int(onesNum))
        tensNum = numString[-2]
        tens = numToWordMap(int(tensNum)*10)
        return tens+ones
    else:
        # TODO
        pass

def numToWordMap(num):
    mapping = {
    0:"",
    1:"one",
    2:"two",
    3:"three",
    4:"four",
    5:"five",
    6:"six",
    7:"seven",
    8:"eight",
    9:"nine",
    10:"ten",
    11:"eleven",
    12:"twelve",
    13:"thirteen",
    14:"fourteen",
    15:"fifteen",
    16:"sixteen",
    17:"seventeen",
    18:"eighteen",
    19:"nineteen",
    20:"twenty",
    30:"thirty",
    40:"fourty",
    50:"fifty",
    60:"sixty",
    70:"seventy",
    80:"eighty",
    90:"ninety",
    100:"onehundred",
    200:"twohundred",
    300:"threehundred",
    400:"fourhundred",
    500:"fivehundred",
    600:"sixhundred",
    700:"sevenhundred",
    800:"eighthundred",
    900:"ninehundred",
    }

    return mapping[num]


if __name__ == '__main__':
    pass
도움이 되었습니까?

해결책

The error means that a function was used where there should have been a list, like this:

def foo(): pass
foo[3]

You must have changed some code.

By the way, wordify(40) returned "fourty". I spell it "forty" And you have no entry for zero

다른 팁

In case someone looks here and has the same problem I had, you can also get a pointer to a function object if the wrong variable name is returned. For example, if you have function like this:

 def foo():
   my_return_val = 0
   return return_val

 my_val = foo()

then my_val will be a pointer to a function object which is another cause to "TypeError: 'function' object is unsubscriptable" if my_val is treated like a list or array when it really is a function object.

The solution? Simple! Fix the variable name in foo that is returned to my_return_val.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top