2014-01-24 47 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] 
相關問題