TypeError:"str"对象无法通过input()调用 [英] TypeError: 'str' object is not callable with input()

查看:52
本文介绍了TypeError:"str"对象无法通过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()时.

更具体地说:

  1. 程序进入getInputFile()函数,并且尚未分配input.这意味着Python解释器将按照您的预期使用内置的input.
  2. 您返回filename并退出getInputFile().现在,解释程序将名称input覆盖为该字符串.
  3. getOutputFile()现在尝试使用input,但已被您的文件名字符串替换.您无法调用字符串,因此解释器会告诉您并抛出错误.
  1. The program enters the getInputFile() function, and input hasn't been assigned yet. That means the Python interpreter will use the built-in input, as you intended.
  2. You return filename and get out of getInputFile(). The interpreter now overwrites the name input to be that string.
  3. getOutputFile() now tries to use input, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆