2014-01-24 72 views
1

由于某种原因,这段脚本正在返回错误:“TypeError:字符串索引必须是整数”;我看不出有什么问题。我是愚蠢的,忽略了一个明显的错误吗?我无法看到一个为我的生活!Python - 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 + " - " + definition 
    print(sentence) 
else: 
    print("Term doesn't exist.") 
+0

耶稣基督,做我觉得愚蠢!谢谢大家指出我正确的方向。 – RoyalSwish

回答

2

您索引串term而不是字典terms的。尝试:

definition = terms[term] 
3

我想你想这样说:definition = terms[term]

3

此行definition = term[terms]正在试图获得一个字符出字符串term的。你可能只是typoed,并希望

definition = terms[term] 
       ^here, reference the dict, not the string 
2

您意外交换的变量。更改此:

definition = term[terms] 

要这样:

definition = terms[term] 
相关问题