A dict concept from ex39 has me vexed n perplexed! (-:

Hello Community,
I’m just getting into coding and have a question for y’all.
I was practicing concepts from exercise 39 and stumbled upon an interesting thing.
The same code that runs fine (line 5) produces an error message later on (line 14)
when it is placed after a ‘for’ command (line 7-9).

Why does this happen? My guess is that I need a command that stops
the ‘for’ command. Any help would be great.

PS- I tried this on the actual ex 39 from the book and put the line in different places
and it only broke the code when placed after the ‘for’ commands


Here’s the code.
screen shot

Here is the error message
Traceback (most recent call last):
File “dicttest .py”, line 14, module
print("Washington has the city: ", cities[states[‘Washington’]])
TypeError: string indices must be integers

This is kind of a strange thing about Python. So see the for states, abbrev in list(states.items()): part?

You are going through the dict of states, and also assigning the results to a variable named states (and abbrev).

That means you’re replacing the original variable states which is a dict, with a new variable named states that is getting updated with a string of the keys. Now when you hit line 14, states is no longer a dict. It’s a string, probably of the last key.

Most likely you meant state not states, as in for state, abbrev in list(states.items()).

Also, there’s sort of a stylistic bug in line 8. You don’t need to do list(states.items()), and can just do states.item() but I think I left some debugging code in there.

Thank you so much! This was driving me a bit crazy. If this is indeed Zed I wanted to let you know that I really enjoy the book and the method of instruction. I teach and play guitar and drums for a living so I can relate to the practice aspect of coding and to your experience of learning guitar as well. Best-David

Yep, this is really me. I am a one man show. :wink:

Hi Zed,
But I’m still getting this mistake tbh and it’s driving me crazy at this stage lol:

Like even if I try to remove “list” as you advised above, I still keep getting “invalid syntax”
and I don’t understand it all why it does seem to compile the normal way all the way till the state when we are trying to do both at the same time (

…guess that is the right explanation; however, it still doesn’t help whether I use state or states initially and then remove list
Please-please help:(

Did it really work!!!
Did I really just miss ‘s’ at the end of item(S)?

Yep, looks like it. Also there’s an error in that code. You don’ t need to do list(states.items()).

All the characters of a string have a unique index . This index specifies the position of each character of the string. TypeError: string indices must be integers means an attempt to access a location within a string using an index that is not an integer.

For example, str[hello"] and str[2.1] as indexes. As these are not integers, a TypeError exception is raised. This means that when you’re accessing an iterable object like a string or float value, you must do it using an integer value .

Python supports slice notation for any sequential data type like lists, strings , tuples, bytes, bytearrays, and ranges. When working with strings and slice notation, it can happen that a TypeError: string indices must be integers is raised, pointing out that the indices must be integers, even if they obviously are.