簡體   English   中英

使用 input() 函數時出現 Python 錯誤 [TypeError: 'str' object is not callable]

[英]Python Error [TypeError: 'str' object is not callable] comes up when using input() funtion

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 (不在函數中)嘗試了這條線,它可以工作,但由於某種原因,它在 function 內不起作用。

誰能給我解釋一下?

line input = input("in or out? ")的問題。

您將input function 重新定義為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)

input = input("in or out? ")覆蓋內置輸入function。 用不同的名稱替換變量名稱,它將起作用。

嘗試這個:

notes =[]
def newNote(notes):
    note = input("Whats up")
    notes.append(note)
    return notes

inp = input("in or out? ")
if (inp == "in"):
    newNote(notes)

您已使用關鍵字“輸入”來命名變量。 除非你想覆蓋語言的內置功能,否則你不應該使用關鍵字來定義函數或變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM