Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

Object of type 'function' has no len() in python

I have been searching for a solution for this error for a while but the solutions that have helped others have not been much help for me.

Here is the code that I've wrote.

def main():
    while True:
        userInput()
        characterCount(userInput)
        middleLetter()
        spaceCount()
        letterReplace()
        displayOutput()


def userInput():
    sentence = str(input('Enter a sentence at least 10 letters long, or type STOP to quit:')) 
    if sentence == 'STOP':
        quit()
    return sentence

def characterCount(sentence):
    characterCount = len(sentence) - sentence.count(' ')
    if characterCount < 10:
        print('Sorry that is less than 10 letters')
    return characterCount

def middleLetter(sentence):
    sentence = len(sentence)/2
    middleLetter = [sentence +1]
    return middleLetter

def spaceCount(sentence):
    spaceCount = sentence.count(' ')
    return spaceCount


def letterReplace(sentence):
    letterReplace= sentence.replace("a", "&")
    return letterReplace


def displayOutput(characterCount,middleLetter,spaceCount,letterReplace):
    print('Number of letters: '(characterCount))
    print('Middle letter: '(middleLetter))
    print('Spaces counted: '(spaceCount))
    print('Sentence with letter replaced: '(letterReplace))


main()

The problem I have is that when I run the program I get the error.

Traceback (most recent call last):
  File "C:UserswoodDesktopSoftware designProgram 4program3_4QuinnWood.py", line 59, in <module>
    main()
  File "C:UserswoodDesktopSoftware designProgram 4program3_4QuinnWood.py", line 18, in main
    characterCount(userInput)
  File "C:UserswoodDesktopSoftware designProgram 4program3_4QuinnWood.py", line 32, in characterCount
    characterCount = len(sentence) - sentence.count(' ')
TypeError: object of type 'function' has no len()

Most of the times I have seen this error is because of a int being used instead of a string but I can not see what would be causing this error. Any help would be appreciated.

Using some of the given suggestions I have fixed the original error but now when I try to run it I receive the error.

Traceback (most recent call last):
  File "C:UserswoodDesktopSoftware designProgram 4program3_4QuinnWood.py", line 59, in <module>
    main()
  File "C:UserswoodDesktopSoftware designProgram 4program3_4QuinnWood.py", line 22, in main
    displayOutput(characterCount,middleLetter,spaceCount,letterReplace)
  File "C:UserswoodDesktopSoftware designProgram 4program3_4QuinnWood.py", line 53, in displayOutput
    print('Number of letters:'(characterCount))
TypeError: 'str' object is not callable
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to capture the output of userInput():

while True:
    sentence = userInput()
    characterCount(sentence)
    ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...