| | |
Dictionary -- states and their capitals
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 34
Reputation:
Solved Threads: 0
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:
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 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)
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!
Last edited by babutche; Jan 1st, 2007 at 3:33 pm. Reason: to correct spelling of a word
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]
[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
•
•
Join Date: Oct 2006
Posts: 34
Reputation:
Solved Threads: 0
Thanks for your input!
I have revised my program and this is how it looks:
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!
I have revised my program and this is how it looks:
Python Syntax (Toggle Plain Text)
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!
![]() |
Similar Threads
- Dictionary ADT (Computer Science)
- VX2 removal (Viruses, Spyware and other Nasties)
- Win32 viruses infecting .exe files - hijacker log (Viruses, Spyware and other Nasties)
Other Threads in the Python Forum
- Previous Thread: get text input from shell cursor
- Next Thread: Encoding declaration warning
Views: 2549 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Python
apache application argv beginner binary calculator character code command compile cursor cx-freeze development dictionaries dictionary dynamic error event examples excel file float format ftp function google gui homework ideas import input java keyboard launcher line linux list lists loop maze microphone mouse movingimageswithpygame newb number numbers obexftp output parsing path permissions phonebook port prime program programming projects py2exe pygame pygtk pyqt pysimplewizard python random recursion recursive refresh return reverse scrolledtext session shebang signal simple sprite ssh string strings table terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 valueerror variable verify voip windows wxpython






