我正在做一个绞刑游戏,我总是遇到同样的错误,我试着调试了几个小时,没有任何进展。
这是错误消息:
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
发布于 2014-11-15 21:20:31
问题在于这一行:
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'
>>>
https://stackoverflow.com/questions/26950849
复制相似问题