Python-TypeError:字符串索引必须是整数
出于某种原因,这段脚本返回错误:“TypeError:字符串索引必须是整数”;我看不出有什么不对。我是不是很愚蠢,忽略了一个明显的错误?我一辈子都看不到Python-TypeError:字符串索引必须是整数,python,typeerror,Python,Typeerror,出于某种原因,这段脚本返回错误:“TypeError:字符串索引必须是整数”;我看不出有什么不对。我是不是很愚蠢,忽略了一个明显的错误?我一辈子都看不到 terms = {"ALU":"Arithmetic Logic Unit"} term = input("Type in a term you wish to see: ") if term in terms: definition = term[terms] sentence = term + " - " + defini
terms = {"ALU":"Arithmetic Logic Unit"}
term = input("Type in a term you wish to see: ")
if term in terms:
definition = term[terms]
sentence = term + " - " + definition
print(sentence)
else:
print("Term doesn't exist.")
您正在索引字符串
术语
,而不是字典术语
。尝试:
definition = terms[term]
我想您是这样想的:
definition=terms[term]
这一行definition=term[terms]
试图从字符串term
中提取一个字符。你可能只是打字,想要
definition = terms[term]
^ here, reference the dict, not the string
您不小心交换了变量。更改此项:
definition = term[terms]
为此:
definition = terms[term]
天哪,我觉得自己很愚蠢!谢谢大家为我指明了正确的方向。