0

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.")
RoyalSwish
  • 1,503
  • 10
  • 31
  • 57

4 Answers4

3

I think you want it this way: definition = terms[term]

Paulo Bu
  • 29,294
  • 6
  • 74
  • 73
3

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
CDspace
  • 2,639
  • 18
  • 30
  • 36
2

You are indexing the string term instead of the dictionary terms. Try:

definition = terms[term]
mdml
  • 22,442
  • 8
  • 58
  • 66
2

You accidentally swapped the variables. Change this:

definition = term[terms]

To this:

definition = terms[term]
Paolo
  • 20,112
  • 21
  • 72
  • 113
nQue
  • 41
  • 4