Learn Python the Hard Way- Exercise 39 Issues via /r/learnprogramming


Learn Python the Hard Way- Exercise 39 Issues

Hi all,

I'm going through "Learn Python the Hard Way" and I can't seem to find the bug in my code, the code is below, sorry as it's a bit long. Tabs are shown with (TAB). With the code is hard to read, it can also be found here: https://repl.it/J6UN/0

Create a mapping of state to abbreviations states= { (TAB)'Oregon' : 'OR', (TAB) 'Florida': 'FL', (TAB)'California':'CA', (TAB)'New York':'NY', (TAB)'Michigan':'MI' }

create a basic set of states and some cities in them cities = { (TAB)'CA': 'San Francisco', (TAB)'MI': 'Detroit', (TAB)'FL': 'Jacksonville' }

cities['NY']= 'New York' cities['OR']= 'Portland'

print '_' * 10 print "NY state has: ", cities['NY'] print "OR state has: ", cities['OR']

print '_' * 10 print "Michigan's abbreviation is: ", states['Michigan'] print "Florida's abbreviation is: ", states['Florida']

print '–' * 10 print "Michigan has: ", cities[states["Michigan"]] print "Florida has: ", cities[states["Florida"]]

print "-" * 10 for state, abbrev in states.items(): (TAB)print "%s is abbreviated %s" % (state, abbrev)

print "-" * 10 for abbrev, cities in cities.items(): (TAB)print "%s has the city %s" % (abbrev, cities)

print "-" * 10 for state, abbrev in states.items(): (TAB)print "%s state is abbreviated %s and has city %s" % ( (TAB)state, abbrev, cities[abbrev])

print "-" * 10 state = state.get('Texas', None)

if not state: (TAB) print "Sorry, no Texas."

city =cities.get('Texas', 'Does Not Exist') print "The city for the state 'TX' is: %s" % city

Every time I run the above code, I get the following error message: Traceback (most recent call last): (TAB)File "python", line 50, in <module> TypeError: string indices must be integers, not str

As the keys are strings, I'm not sure why I'm getting this error. When I copy and paste Zed's code, I don't get the same error, so the issue is on my end. Any ideas what the issue might be? Thanks for any help in advance

Submitted July 17, 2017 at 10:12PM by 123zxcfgh
via reddit http://ift.tt/2u43QhV

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s