【问题标题】:Python Error [TypeError: 'str' object is not callable] comes up when using input() funtion使用 input() 函数时出现 Python 错误 [TypeError: 'str' object is not callable]
【发布时间】:2019-10-20 07:41:36
【问题描述】:
notes =[]

def newNote(notes):

    note = input("Whats up")
    notes.append(note)
    return notes

input = input("in or out? ")

if (input == "in"):

    newNote(notes)

note = input("Whats up") 是有问题的行,我看不出有什么问题。我只是通过 instelf (不在函数中)尝试了该行,它可以工作,但由于某种原因,它在函数内部不起作用。

谁能给我解释一下?

【问题讨论】:

    标签: python input callable


    【解决方案1】:

    input = input("in or out? ") 行的问题。

    您将input 函数重新定义为input("in or out? ") 的结果,所以现在input 是一个字符串。

    解决方案是简单地将input("in or out? ") 结果变量更改为另一个变量:

    notes =[]
    
    def newNote(notes):
    
        note = input("Whats up")
        notes.append(note)
        return notes
    
    choice = input("in or out? ")
    
    if (choice == "in"):
    
        newNote(notes)
    

    【讨论】:

      【解决方案2】:

      input = input("in or out? ") 正在覆盖内置的 input 函数。 将变量名替换为其他名称即可。

      【讨论】:

        【解决方案3】:

        试试这个:

        notes =[]
        def newNote(notes):
            note = input("Whats up")
            notes.append(note)
            return notes
        
        inp = input("in or out? ")
        if (inp == "in"):
            newNote(notes)
        

        您已使用关键字“输入”来命名变量。除非你想覆盖语言的内置功能,否则你不应该使用关键字来定义函数或变量。

        【讨论】:

          猜你喜欢
          • 2019-10-08
          • 1970-01-01
          • 2017-01-23
          • 1970-01-01
          • 2021-02-03
          • 1970-01-01
          • 2016-03-25
          • 2011-04-26
          相关资源
          最近更新 更多