由于某种原因,这段脚本返回错误:“TypeError:string indices必须是整数”;我看不出有什么问题。我是愚蠢的,在这里忽略了一个明显的错误吗?我不能为我的生活看到一个!
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.")
答案 0 :(得分:3)
我认为你想要这样:definition = terms[term]
答案 1 :(得分:3)
此行definition = term[terms]
正在尝试从字符串term
中获取字符。你可能只是打错了,想要
definition = terms[term]
^ here, reference the dict, not the string
答案 2 :(得分:2)
您正在索引字符串term
而不是字典terms
。尝试:
definition = terms[term]
答案 3 :(得分:2)
您不小心交换了变量。改变这个:
definition = term[terms]
对此:
definition = terms[term]