19

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • possible duplicate of [TypeError: 'int' object is not callable](http://stackoverflow.com/questions/9767391/typeerror-int-object-is-not-callable) – Cees Timmerman Dec 08 '14 at 17:30

3 Answers3

54

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:
    
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    Had similar error that was really hard to find - I used len as variable in a for loop. Pyhton should really add some more information for cases like this. – user1612250 Apr 27 '18 at 13:56
  • @user1612250: `len` is just another built-in name; there are quite a lot such names, and new names are added from time to time. It's perfectly fine to use such names in your own code for other purposes! Re-binding names to something else is no bigger problem with such names than with any other names you use in your code. That said, a good IDE can help here, or you could add [type hints](https://www.python.org/dev/peps/pep-0483/) to your code and have a tool like mypy or PyCharm catch issues like these that way. – Martijn Pieters Apr 27 '18 at 16:18
  • Had the same issue too, where I'd passed an argument to function in with name len.. WIll remember this one. Really had me stumped. Is there perhaps, a lint warning you can enable, warning of overridden default names? – JGFMK May 30 '18 at 15:47
  • @JGFMK: yes, use the [flake8-builtins plugin](https://github.com/gforcada/flake8-builtins) for Flake8. – Martijn Pieters May 30 '18 at 18:37
  • @MartijnPieters - Haven't used . that. I got some advice here too: https://stackoverflow.com/q/50609715/495157 and worked out a way of doing it with pylint - since that's what my IDE, VS Code seems to default too. Thanks though :-) – JGFMK May 30 '18 at 18:39
1

My mistake was that I have initialized len=0 somewhere in my code. so don't make variables on the name of keywords

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 11 '22 at 15:07
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31745354) – James Barnett May 16 '22 at 15:17
1

Just use del len and initialize with a new (non-keyword) variable, it should work fine.

Prachi
  • 31
  • 1
  • 4
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 13 '22 at 12:14
  • Worked for me, deleted the len variable and then used it to find len of dataframe – asonagra Jan 13 '23 at 20:57