错误:' str'对象不可调用python

时间:2017-11-18 10:16:18

标签: python

我一直在处理python中的错误并且已经搜索了一段时间来修复它但无济于事。

这是我得到的错误

Traceback (most recent call last):
  File "C:\Users\wood\Desktop\Software design\Program 4\program3_4QuinnWood.py", line 62, in <module>
    main()
  File "C:\Users\wood\Desktop\Software design\Program 4\program3_4QuinnWood.py", line 23, in main
    displayOutput(letterCount,middleCharacter,spaceAmount,aReplace)
  File "C:\Users\wood\Desktop\Software design\Program 4\program3_4QuinnWood.py", line 56, in displayOutput
    print('Number of letters:'(letterCount))
TypeError: 'str' object is not callable

这是我到目前为止编写的代码。

def main():

    while True:
        sentence= userInput()
        letterCount= characterCount(sentence)
        middleCharacter= middleLetter(sentence)
        spaceAmount= spaceCount(sentence)
        aReplace= letterReplace(sentence)
        displayOutput(letterCount,middleCharacter,spaceAmount,aReplace)


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):
    letterCount = len(sentence) - sentence.count(' ')
    if letterCount < 10:

        print('Sorry that is less than 10 letters')


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


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



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



def displayOutput(letterCount,middleCharacter,spaceAmount,aReplace):
    print('Number of letters:'(letterCount))
    print('Middle letter:'(middleCharacter))
    print('Spaces counted:'(spaceAmount))
    print('Sentence with letter replaced:'(aReplace))

main()

解决方案可能很简单,我忽略了,但任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

错误状态为str object not callable,这意味着您正在处理函数中的字符串或代码中可调用()的任何内容。

问题出在displayOutput()函数

的打印语句中

更正后的代码

def displayOutput(letterCount,middleCharacter,spaceAmount,aReplace):
    print('Number of letters:',letterCount)
    print('Middle letter:',middleCharacter)
    print('Spaces counted:',spaceAmount)
    print('Sentence with letter replaced:',aReplace)

main()