“function”类型的对象在python中没有len()

时间:2022-02-28 17:08:10

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:\Users\wood\Desktop\Software design\Program 4\program3_4QuinnWood.py", line 59, in <module>
    main()
  File "C:\Users\wood\Desktop\Software design\Program 4\program3_4QuinnWood.py", line 18, in main
    characterCount(userInput)
  File "C:\Users\wood\Desktop\Software design\Program 4\program3_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.

我看到这个错误的大多数时候是因为使用了int而不是string,但是我不知道是什么导致了这个错误。如有任何帮助,我们将不胜感激。

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:\Users\wood\Desktop\Software design\Program 4\program3_4QuinnWood.py", line 59, in <module>
    main()
  File "C:\Users\wood\Desktop\Software design\Program 4\program3_4QuinnWood.py", line 22, in main
    displayOutput(characterCount,middleLetter,spaceCount,letterReplace)
  File "C:\Users\wood\Desktop\Software design\Program 4\program3_4QuinnWood.py", line 53, in displayOutput
    print('Number of letters:'(characterCount))
TypeError: 'str' object is not callable

3 个解决方案

#1


0  

You need to capture the output of userInput():

您需要捕获userInput()的输出:

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

#2


0  

You are trying to call the function here with a function as an argument.

你尝试用函数来调用函数作为参数。

userInput()
characterCount(userInput)

Instead capture the return value in a variable and call the other function with the variable as an argument.

相反,捕获变量中的返回值,并以变量作为参数调用另一个函数。

Example:

例子:

def f():
    return 4

def c(f):
    return f 

x = c(f) # <function f at 0x00000231D4063A60>
print(dir(x))   

# ['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

which doesn't have len()

没有len()

#3


0  

In Yours code the object userInput has no len(). But object userInput() - has.

在您的代码中,对象userInput没有len()。但是对象userInput() -有。

#1


0  

You need to capture the output of userInput():

您需要捕获userInput()的输出:

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

#2


0  

You are trying to call the function here with a function as an argument.

你尝试用函数来调用函数作为参数。

userInput()
characterCount(userInput)

Instead capture the return value in a variable and call the other function with the variable as an argument.

相反,捕获变量中的返回值,并以变量作为参数调用另一个函数。

Example:

例子:

def f():
    return 4

def c(f):
    return f 

x = c(f) # <function f at 0x00000231D4063A60>
print(dir(x))   

# ['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

which doesn't have len()

没有len()

#3


0  

In Yours code the object userInput has no len(). But object userInput() - has.

在您的代码中,对象userInput没有len()。但是对象userInput() -有。