TypeError: 'int' object is not callable,,, len()

跟風遠走 提交于 2019-12-21 07:48:06

问题


I wrote a program to play hangman---it's not finished but it gives me an error for some reason...

import turtle
n=False
y=True
list=()
print ("welcome to the hangman! you word is?")
word=raw_input()
len=len(word)
for x in range(70):
    print
print "_ "*len
while n==False:
    while y==True:
        print "insert a letter:"
        p=raw_input()
        leenghthp=len(p)
        if leengthp!=1:
            print "you didnt give me a letter!!!"
        else:
            y=False
    for x in range(len):
        #if wo
        print "done"

error:

    leenghthp=len(p)
TypeError: 'int' object is not callable

回答1:


You assigned to a local name len:

len=len(word)

Now len is an integer and shadows the built-in function. You want to use a different name there instead:

length = len(word)
# other code
print "_ " * length

Other tips:

  • Use not instead of testing for equality to False:

    while not n:
    
  • Ditto for testing for == True; that is what while already does:

    while y:
    


来源:https://stackoverflow.com/questions/17762210/typeerror-int-object-is-not-callable-len

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!