Computer Science, asked by npschauhan4, 4 months ago

Create a dictionary to store names of states and their capitals.​

Answers

Answered by bmangroliya10
2

Answer:

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!

Explanation:

plz mark me as brilliant

Answered by viji18net
1

Answer:

state_capital={}

num=int(input("Enter the number of states and capitals:"))

for i in range (num):

   state=input("Enter the state:")

   capital=input("Enter the capital:")

   state_capital[state]=capital

print("the created stat eand capital dictionary is:",state_capital)

Similar questions