Hacking the Fender, "String indices must be integers"

Hi everyone,

First time poster so apologies if I’m not doing it right here!

So I’m on step 7 of “Hacking the Fender” in the Python 3 Course.

I was feeling quite pleased with myself until I got to points 6 and 7 and I cannot seem to clear up an issue, I even watched the developers tutorial to see where I was going wrong, but from my understanding I am doing what they are doing to the letter!

import csv

compromised_users =

with open(“passwords.csv”) as password_file:

password_csv = csv.DictReader(password_file)

for password_row in password_file:

compromised_users.append(password_row['Username'])

This is returning:

Traceback (most recent call last):
File “script.py”, line 8, in
compromised_users.append(password_row[‘Username’])
TypeError: string indices must be integers

Which I haven’t seen before. I’ve come to the end of thoughts of how to do this, so if anyone can point out the probably obvious mistake I am making then please, I’d very much appreciate it!

1 Like

Welcome to the forums!! :tada:

The clue is in the error TypeError: string indices must be integers.

The culprit being that the element that you’re iterating with password_row, is a string.

Depending on what you need, you might want to try putting the integer that refers to the ‘Username’ slot. Or the type of your iterator would have to change…

The other possibility is if the list is of strings it won’t work, but it was supposed to be dictionaries… That could work…

dict_list = {'hello':'world'}
print(list['hello'])
# outputs: 'world'

compared to

list = ['hello', 'world']
print(list['hello'])
# TypeError: list indices must be integers or slices, not str

Thank you for the help! Now that I’ve rectified the blinding error I knew was going to be and changed it to

for password_row in password_csv:

The job is a goodun, thank you!

1 Like