"TypeError: string indices must be integers" While Looking Up a Key in Dictionary

How come in this code:

db = {'item1': 'thing1', 'item2': 'thing2'}
user = 'item1'
print(db[user])

I get this error:

TypeError: string indices must be integers

I’m almost certain it just started throwing this error today because it was working fine last week.

I asked ChatGPT and it said I need to use a integer index instead of finding the value by its key. What is the point of using a dictionary if I need to look up a value by its index instead of its key? Maybe I’m missing something?

That should work. Try creating a new repl and running it.

Hmm, it does work.

Basically I have a Repl with ReplitDB (db) where all keys’ values are a dictionary.
This is where the error is called from:

user = "BusyBird15"
user = user.lower()
bal = float(db[user]["balance"]) #Gives TypeError for a string and says it should be an int
out = {"balance": bal, "username": user, "url": url}

The database structure is this:

{
'BusyBird15': {balance: 150,
               name: "Mr. Nachos"},
'S4IL': {balance: 150,
         name: "Mrs. Cheese"},
}

Weirder yet, if the user variable is “BusyBird15”, it works fine. If it is “S4IL”, it throws the error.

1 Like

May I see the exact error and have the repl link?

My friend got it working, he said it rendered a few of the dictionary objects as strings instead of dictionaries. He deleted and reassigned dictionary objects to the keys that were not working. I don’t know how this happened, but it works fine now. :person_shrugging:

Well I guess it is not that, this code gives the same error:

db = {'first': {'bal': 105, 'password': ''}, 'first': {'bal': 190, 'password': 'eeerrrer'}, 'last': {'bal': 1045, 'password': ''}} 

for i in db.keys():
    try: 
        if i['password'] == "":
            del db[i]
    except KeyError:
        print("Skipped one due to no password")
print("Removal complete")
print(db)

Are you sure this is the code? You have two entries for the key 'first' . The variable i in the loop is iterating over keys. There are some other errors too.

I made some adjustments, try again with this:

db = {'first': {'bal': 105, 'password': ''}, 'second': {'bal': 190, 'password': 'eeerrrer'}, 'last': {'bal': 1045, 'password': ''}}

keys_to_remove = []
for i in db.keys():
    try: 
        if db[i]['password'] == "":
            keys_to_remove.append(i)
    except KeyError:
        print("Skipped one due to no password")

for key in keys_to_remove:
    del db[key]

print("Removal complete")
print(db)
1 Like

Well, same error.
This is what I used:

keys_to_remove = []
for i in db.keys():
    try: 
        if db[i]['password'] == "":
            keys_to_remove.append(i)
            print("Removed a user! (Blank Password)")
    except KeyError:
        keys_to_remove.append(i)
        print("Removed a user! (No Password)")
for key in keys_to_remove:
    del db[key]
print("Removal complete")

And got:
image

(db was ReplitDB)

It worked fine with db as a variable, but with db as ReplitDB it throws the error.

Try to check whats inside the db, add a print.

for i in db.keys():
    print(i, db[i])

And compare the values to see if it is what you expect.

1 Like