TypeError:"str"对象无法通过input()调用 [英] TypeError: 'str' object is not callable with input()
问题描述
我有以下代码,应该询问用户2个文件名.我在第二个函数中遇到了input()的错误,但在第一个函数中却没有,我不明白... 这是错误:
I have the following code, which is supposed to ask the user 2 file names. I get an error with the input() in the second function but not in the first, I don't understand... Here is the error :
输出= getOutputFile() getOutputFile中的文件"splitRAW.py",第22行 fileName = input("\ t =>") TypeError:"str"对象不可调用
output = getOutputFile() File "splitRAW.py", line 22, in getOutputFile fileName = input("\t=> ") TypeError: 'str' object is not callable
# 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():
And here is my main() :
if __name__ == "__main__":
input = getInputFile()
output = getOutputFile()
推荐答案
问题是当您说input = getInputFile()
时.
更具体地说:
- 程序进入
getInputFile()
函数,并且尚未分配input
.这意味着Python解释器将按照您的预期使用内置的input
. - 您返回
filename
并退出getInputFile()
.现在,解释程序将名称input
覆盖为该字符串. -
getOutputFile()
现在尝试使用input
,但已被您的文件名字符串替换.您无法调用字符串,因此解释器会告诉您并抛出错误.
- The program enters the
getInputFile()
function, andinput
hasn't been assigned yet. That means the Python interpreter will use the built-ininput
, as you intended. - You return
filename
and get out ofgetInputFile()
. The interpreter now overwrites the nameinput
to be that string. getOutputFile()
now tries to useinput
, but it's been replaced with your file name string. You can't call a string, so the interpreter tells you that and throws an error.
尝试将input = getInputFile()
替换为其他变量,例如fileIn = getInputFile()
.
Try replacing input = getInputFile()
with some other variable, like fileIn = getInputFile()
.
此外,您的getOutputFile()
不返回任何内容,因此您的output
变量将只包含None
.
Also, your getOutputFile()
is not returning anything, so your output
variable will just have None
in it.
这篇关于TypeError:"str"对象无法通过input()调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!