Dictionary -- states and their capitals

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2006
Posts: 34
Reputation: babutche is an unknown quantity at this point 
Solved Threads: 0
babutche babutche is offline Offline
Light Poster

Dictionary -- states and their capitals

 
0
  #1
Jan 1st, 2007
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:

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Dictionary -- states and their capitals

 
0
  #2
Jan 1st, 2007
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.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,297
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 178
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Dictionary -- states and their capitals

 
0
  #3
Jan 2nd, 2007
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.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 34
Reputation: babutche is an unknown quantity at this point 
Solved Threads: 0
babutche babutche is offline Offline
Light Poster

Re: Dictionary -- states and their capitals

 
0
  #4
Jan 2nd, 2007
Thanks for your input!

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

  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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,297
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 178
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Dictionary -- states and their capitals

 
0
  #5
Jan 2nd, 2007
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.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 34
Reputation: babutche is an unknown quantity at this point 
Solved Threads: 0
babutche babutche is offline Offline
Light Poster

Re: Dictionary -- states and their capitals

 
0
  #6
Jan 2nd, 2007
O.K. I get it! Thanks!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 2549 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC