TypeError:类型为' builtin_function_or_method'的对象在代码的两部分中没有len()

时间:2014-11-15 21:19:35

标签: python

我正在制作一个刽子手游戏,我一直遇到同样的错误,我一直试图调试它几个小时但没有进展。

这是错误消息:

Traceback (most recent call last):
   File "hangman.py", line 128, in <module>
       guess = guessletter(miss + correct)
   File "hangman.py", line 103, in guessletter
       if len(guess) != 1:
   TypeError: object of type 'builtin_function_or_method' has no len()

以下是我的代码的相关部分:

第98行 - 110

`def guessletter(previousguess):   #this function lets the player guess a letter, and see if the guess is acceptable
    while True:
        print ('Guess a Letter')
        guess = input()
        guess = guess.lower     
        if len(guess) != 1:
            print ('Enter single letter please.')
        elif guess in previousguess:
            print ('That letter was already guessed. Choose another')
        elif guess not in 'abcdefghijklmnopqrstuvwxyz':
            print ('Please put a letter')
        else:
            return guess`

第125~141行

while True:
board(hangmanpictures, miss, correct, unknownword)   #i define a board function at the top

guess = guessletter(miss + correct)   #i use the function defined above, but it seems to make an error here..

if guess in unknownword:
    correct = correct + guess

    foundallletters = True    #check if player has won
    for k in range(len(unknownword)):
        if unknownword[k] not in correct:
            foundallletters = False
            break

    if foundallletters:
        print ('The secret word is "' + unknownword + '"! You won!')
        gamefinish = True

1 个答案:

答案 0 :(得分:4)

问题在于这一行:

guess = guess.lower

您忘记调用str.lower方法,因此guess被分配给方法对象本身。

要解决此问题,请将()放在其名称之后调用该方法:

guess = guess.lower()
#                  ^^

以下是演示:

>>> guess = 'ABCDE'
>>> guess = guess.lower
>>> guess
<built-in method lower of str object at 0x014FA740>
>>>
>>> guess = 'ABCDE'
>>> guess = guess.lower()
>>> guess
'abcde'
>>>