Python - 类型错误:字符串索引必须是整数 [英] Python - TypeError: string indices must be integers
本文介绍了Python - 类型错误:字符串索引必须是整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于某种原因,这段脚本返回了错误:TypeError: 字符串索引必须是整数";我看不出有什么问题.我是不是很愚蠢,忽略了一个明显的错误?我这辈子都看不到了!
For some reason this piece of script is returning the error: "TypeError: string indices must be integers"; and I cannot see what's wrong. Am I being stupid and overlooking an obvious mistake here? I can't see one for the life of me!
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 = term[terms]
试图从字符串 term
中取出一个字符.你可能只是打错了,想要
This line definition = term[terms]
is trying to get a character out of the string term
. You probably just typoed, and want
definition = terms[term]
^ here, reference the dict, not the string
这篇关于Python - 类型错误:字符串索引必须是整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文