944,074 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2774
  • Python RSS
Jan 1st, 2007
0

Dictionary -- states and their capitals

Expand Post »
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:

Python Syntax (Toggle Plain Text)
  1. def main():
  2. staStr = raw_input("Enter the name of a state (Enter quit to exit): ")
  3. cs = {"indianapolis":"indiana", "columbus":"ohio", "jackson":"mississippi",
  4. "phoenix":"arizona", "honolulu":"hawaii", "richmond":"virginia",
  5. "springfield":"illnois", "lincoln":"nebraska",
  6. "boston":"massachuettes", "lansing":"michigan", "desmoines": "iowa",
  7. "salem": "oregon"}
  8. while staStr != "quit":
  9. if cs.has_key(staStr):
  10. cs.get(staStr, "quit")
  11. print staStr
  12. else:
  13. print "No key matches! Try again!"
  14.  
  15.  
  16. 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!
Last edited by babutche; Jan 1st, 2007 at 3:33 pm. Reason: to correct spelling of a word
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
babutche is offline Offline
34 posts
since Oct 2006
Jan 1st, 2007
0

Re: Dictionary -- states and their capitals

I would use the state as the key just in case there could be two capitol cities with the same name:
[php]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()
[/php]
Last edited by Ene Uran; Jan 1st, 2007 at 4:48 pm.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Jan 2nd, 2007
0

Re: Dictionary -- states and their capitals

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.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Jan 2nd, 2007
0

Re: Dictionary -- states and their capitals

Thanks for your input!

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

Python Syntax (Toggle Plain Text)
  1. cs = {'indiana': 'indianapolis', 'ohio': 'columbus', 'mississippi': 'jackson',
  2. 'arizona': 'phoenix', 'hawaii': 'honolulu', 'virginia': 'richmond',
  3. 'illnois': 'springfield', 'nebraska': 'lincoln', 'oregon': 'salem',
  4. 'michigan': 'lansing', 'iowa': 'desmoines', 'massachuettes': 'boston'}
  5. def main():
  6. staStr = raw_input("Enter the name of a state (Enter quit to exit): ")
  7. while staStr != "quit":
  8. if cs.has_key(staStr):
  9. cs.get(staStr, "quit")
  10. print "The capital is: ", cs[staStr]
  11. staStr = raw_input("Enter the name of a state"
  12. "(Enter quit to exit): ")
  13. else:
  14. print "No key matches! Try again!"
  15. staStr = raw_input("Enter the name of a state"
  16. "(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!
Reputation Points: 10
Solved Threads: 0
Light Poster
babutche is offline Offline
34 posts
since Oct 2006
Jan 2nd, 2007
0

Re: Dictionary -- states and their capitals

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.
Last edited by sneekula; Jan 2nd, 2007 at 11:16 am.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Jan 2nd, 2007
0

Re: Dictionary -- states and their capitals

O.K. I get it! Thanks!
Reputation Points: 10
Solved Threads: 0
Light Poster
babutche is offline Offline
34 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: get text input from shell cursor
Next Thread in Python Forum Timeline: Encoding declaration warning





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC