Python TypeError: 'int' 对象不可调用,,, len()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17762210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:08:14  来源:igfitidea点击:

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

pythonpython-2.7int

提问by Gilad Wharton Kleinman

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

采纳答案by Martijn Pieters

You assigned to a local name len:

您分配了一个本地名称len

len=len(word)

Now lenis an integer and shadows the built-in function. You want to use a differentname there instead:

Nowlen是一个整数并隐藏内置函数。您想在那里使用不同的名称:

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

Other tips:

其他提示:

  • Use notinstead of testing for equality to False:

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

    while y:
    
  • 使用not而不是测试是否相等于False

    while not n:
    
  • 同上用于测试== True;这就是while已经做到的

    while y: