Hello,

I am trying to create a program that uses a dictionary to associate American states with their capitals. It has an interactive loop that exits when the user enters "quit". The user types the name of the state and the program responds with the state's capital.

I will show you what I have so far and then ask my question.

This is what I have:

def main():
    staStr = raw_input("Enter the name of a state (Enter quit to exit): ")
    cs = {"indianapolis":"indiana", "columbus":"ohio", "jackson":"mississippi",
          "phoenix":"arizona", "honolulu":"hawaii", "richmond":"virginia",
          "springfield":"illnois", "lincoln":"nebraska",
          "boston":"massachuettes", "lansing":"michigan", "desmoines": "iowa",
          "salem": "oregon"}
    while staStr != "quit":
        if cs.has_key(staStr):
            cs.get(staStr, "quit")
            print staStr
        else:
            print "No key matches! Try again!"
 
 
main()

I am not really sure how to associate each state with it's own capital. I know that dictionaries do not have any sequential order therefore I cannot number each element.

I am not really clear on how to do this . Also do I have my states and capitals backwards? Should I have my state as the "key" and my capital as the "value"?

Thanks for your input!

I would use the state as the key just in case there could be two capitol cities with the same name:

dic = {'mississippi': 'jackson', 'arizona': 'phoenix', 'iowa': 'desmoines', 
'massachuettes': 'boston', 'michigan': 'lansing', 'virginia': 'richmond', 
'oregon': 'salem', 'hawaii': 'honolulu', 'nebraska': 'lincoln', 
'indiana': 'indianapolis', 'ohio': 'columbus', 'illnois': 'springfield'}

while True:
    state = raw_input("Enter the name of a state (enter quit to exit): ").lower()
    if state == 'quit':
        break
    try:
        print "The capitol city of %s is %s" % (state.capitalize(), dic[state].capitalize())
    except KeyError:
        print "%s is not in the dictionary!" % state.capitalize()

As far as I know in the US all the states and capitals are unique. Since your are entering the state and want to find that capital, Ene's solution will be the easiest.

Thanks for your input!

I have revised my program and this is how it looks:

cs = {'indiana': 'indianapolis', 'ohio': 'columbus', 'mississippi': 'jackson',
      'arizona': 'phoenix', 'hawaii': 'honolulu', 'virginia': 'richmond',
      'illnois': 'springfield', 'nebraska': 'lincoln', 'oregon': 'salem',
      'michigan': 'lansing', 'iowa': 'desmoines', 'massachuettes': 'boston'}     
def main():
    staStr = raw_input("Enter the name of a state (Enter quit to exit): ")
    while staStr != "quit":
        if cs.has_key(staStr):
            cs.get(staStr, "quit")
            print "The capital is: ", cs[staStr]
            staStr = raw_input("Enter the name of a state"
                               "(Enter quit to exit): ")
        else:
            print "No key matches! Try again!"
            staStr = raw_input("Enter the name of a state"
                               "(Enter quit to exit): ")

I still need to add capital letters, but I think I know how to do that. My whole problem was that I did not understand how to connect the state with the capital(city). For instance, if the state is Indiana and you want it to look up Indianapolis.

I noticed in Ene Uran's example that he used the name of the dictionary (dic) and the state (which was the input) in brackets like these []. I don't understand why? Can anyone explain please? Thank you!

Thanks for the help!

In Ene's example if you code print dic['michigan'] your result would be the value 'lansing'. That is the way you access dictionaries. In other words, the key is the index.

O.K. I get it! Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.