Object of type function has no len() in python

2024/11/19 12:42:19

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 sentencedef characterCount(sentence):characterCount = len(sentence) - sentence.count(' ')if characterCount < 10:print('Sorry that is less than 10 letters')return characterCountdef middleLetter(sentence):sentence = len(sentence)/2middleLetter = [sentence +1]return middleLetterdef spaceCount(sentence):spaceCount = sentence.count(' ')return spaceCountdef letterReplace(sentence):letterReplace= sentence.replace("a", "&")return letterReplacedef 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 maincharacterCount(userInput)File "C:\Users\wood\Desktop\Software design\Program 4\program3_4QuinnWood.py", line 32, in characterCountcharacterCount = 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:\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 maindisplayOutput(characterCount,middleLetter,spaceCount,letterReplace)File "C:\Users\wood\Desktop\Software design\Program 4\program3_4QuinnWood.py", line 53, in displayOutputprint('Number of letters:'(characterCount))
TypeError: 'str' object is not callable
Answer

You need to capture the output of userInput():

while True:sentence = userInput()characterCount(sentence)...
https://en.xdnf.cn/q/119951.html

Related Q&A

My If condition within a while loop doesnt break the loop

Struggling to get my code for the final room to finish the text-based game assigned to me. I essentially want the player to be forced to get all 6 items prior to entry. Any help is greatly appreciated.…

Adding strings, first, first + second, etc

Program has to be able adding strings from the list and output them in sequence but in the way: 1 string 1 + 2 string 1 + 2 + 3 string ...def spacey(array):passe = ""i = ""m = []for…

Store Variables in Lists python

So I have tried to do this, and I think its clear what I want, I want to store the message variables that I have made in a List and then use this for printing, I wonder why does this not work?items = …

How can I kill the explorer.exe process?

Im writing a script which is meant to kill explorer.exe. I searched a bit about it and the best answer Ive seen uses the taskkill command. I tried it, but when I run it on my computer it says it worked…

A presence/activity set command?

So, I was wondering if there could be a command I could write that allows me to set the bots presence and activity (ex. ~~set presence idle or ~~set activity watching "people typing ~~help") …

Inverted Index in Python not returning desired results

Im having trouble returning proper results for an inverted index in python. Im trying to load a list of strings in the variable strlist and then with my Inverse index looping over the strings to return…

Remove white space from an image using python

There are multiple images that have white spaces that I need to remove. Simply crop the image so as to get rid of the white spaces Heres the code I tried so far (this is a result of search) import nump…

How to calculate the ratio between two numbers in python

I have to calculate the ratio between 0.000857179311146189 and 0.026955533883055983 but am unsure how to do this other than by dividing the two numbers. Is it possible to calculate this with the result…

Built-in variable to get current function

I have a lot of functions like the following, which recursively call themselves to get one or many returns depending on the type of the argument: def get_data_sensor(self, sensorname):if isinstance(sen…

Python run from subdirectory

I have the following file hierarchy structure:main.py Main/A/a.pyb.pyc.pyB/a.pyb.pyc.pyC/a.pyb.pyc.pyFrom main.py I would like to execute any of the scripts in any of the subfolders. The user will pass…