Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类型错误:';str';对象不可调用。处理输入()_Python - Fatal编程技术网

Python 类型错误:';str';对象不可调用。处理输入()

Python 类型错误:';str';对象不可调用。处理输入(),python,Python,为什么我会出现这个错误,我该如何修复这个错误 import json from difflib import SequenceMatcher from difflib import get_close_matches data = json.load(open("data.json")) def translate(word): match = get_close_matches(word,data.keys(),cutoff=0.8) if word.lower() in

为什么我会出现这个错误,我该如何修复这个错误

import json
from difflib import SequenceMatcher
from difflib import get_close_matches

data = json.load(open("data.json"))

def translate(word):
    match = get_close_matches(word,data.keys(),cutoff=0.8)
    if word.lower() in data:
        return data[word.lower()]
    elif len(match) > 1:
        yn = input("Did you mean %s? Enter y or n" %match[0])
    else:
        return "Not in the dictionary. Please check again."

input = input("What is your word? ")

print(translate(input))`

What is your word? caca
Traceback (most recent call last):
  File "dictionary.py", line 18, in <module>
    print(translate(input))
  File "dictionary.py", line 12, in translate
    yn = input("Did you mean %s? Enter y or n" %match[0])
TypeError: 'str' object is not callable
导入json
从difflib导入SequenceMatcher
从difflib导入获取\u关闭\u匹配
data=json.load(打开(“data.json”))
def翻译(word):
match=get\u close\u匹配(word,data.keys(),cutoff=0.8)
如果数据中有word.lower():
返回数据[word.lower()]
elif len(匹配)>1:
yn=输入(“您是说%s吗?输入y还是n”%match[0])
其他:
return“不在字典中,请重新检查”
输入=输入(“你的词是什么?”)
打印(翻译(输入))`
你说什么?卡卡
回溯(最近一次呼叫最后一次):
文件“dictionary.py”,第18行,在
打印(翻译(输入))
翻译文件“dictionary.py”,第12行
yn=输入(“您是说%s吗?输入y还是n”%match[0])
TypeError:“str”对象不可调用

您似乎为
输入分配了一个字符串<代码>输入=输入(“你的词是什么?”)
是问题所在。重命名变量,
input
是保留的…
input
是Python中的变量。通常,它包含对提示CLI用户输入一行文本的函数的引用。当您编写
input(“…”
)时,它尝试调用
input
的值,这通常是一个函数,一切正常。但随后您编写了
input=…
,并为
input
分配了一个新值,一个字符串。因此,下一次它进入
input(“…”)
时,它尝试调用字符串。但这不起作用,因为“str”对象不可调用。您应该使用内容管理器来处理文件对象。我建议使用f字符串进行字符串格式化。还要注意,
yn
变量是无用的,因为它会立即被丢弃。您为
input
分配了一个字符串<代码>输入=输入(“你的词是什么?”)是问题所在。重命名变量,
input
是保留的…
input
是Python中的变量。通常,它包含对提示CLI用户输入一行文本的函数的引用。当您编写
input(“…”
)时,它尝试调用
input
的值,这通常是一个函数,一切正常。但随后您编写了
input=…
,并为
input
分配了一个新值,一个字符串。因此,下一次它进入
input(“…”)
时,它尝试调用字符串。但这不起作用,因为“str”对象不可调用。您应该使用内容管理器来处理文件对象。我建议使用f字符串进行字符串格式化。还要注意,
yn
变量是无用的,因为它会立即被丢弃。