簡體   English   中英

TypeError:“ str”對象無法通過input()調用

[英]TypeError: 'str' object is not callable with input()

我有以下代碼,應該詢問用戶2文件名。 我在第二個函數中遇到了input()的錯誤,但在第一個函數中卻沒有,我不明白...這是錯誤:

輸出= getOutputFile()在getOutputFile中的文件“ splitRAW.py”,第22行,fileName = input(“ \\ t =>”)TypeError:'str'對象不可調用

# Loops until an existing file has been found
def getInputFile():
    print("Which file do you want to split ?")
    fileName = input("\t=> ")
    while 1:
        try:
            file = open(fileName, "r")
            print("Existing file, let's continue !")
            return(fileName)
        except IOError:
            print("No such existing file...")
            print("Which file do you want to split ?")
            fileName = input("\t=> ")

# Gets an output file from user
def getOutputFile():
    print("What name for the output file ?")
    fileName = input("\t=> ")

這是我的main():

if __name__ == "__main__":
    input = getInputFile()
    output = getOutputFile()

問題是當您說input = getInputFile()

進一步來說:

  1. 程序進入getInputFile()函數,但尚未分配input 這意味着Python解釋器將按照您的預期使用內置input
  2. 您返回filename並退出getInputFile() 現在,解釋器將input的名稱覆蓋為該字符串。
  3. getOutputFile()現在嘗試使用input ,但是已被您的文件名字符串替換。 您無法調用字符串,因此解釋器會告訴您並拋出錯誤。

嘗試用其他變量替換input = getInputFile() ,例如fileIn = getInputFile()

另外,您的getOutputFile()不返回任何內容,因此您的output變量將只包含None

根據您使用的python版本

Python 2:

var = raw_input("Please enter something: ")
print "you entered", var

或對於Python 3:

var = input("Please enter something: ")
print("You entered: " + var)

暫無
暫無

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

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