Computer Science, asked by doc110010, 10 days ago

Write a program in python to input five state names and their capital in dictionary as a key value pair then ask the user to enter state and print it capital if found in a dictionary otherwise print message state not found in dictionary.​

Answers

Answered by anindyaadhikari13
4

Answer:

The given cσde is written in Python 3.

d=dict()

print("Enter 5 state names with capital.")

for i in range(5):

   s=input("Enter State: ")

   c=input("Enter Capital: ")

   print()

   d[s]=c

   

x=input("Now, enter a state name: ")

if x in d.keys():

   print("Found.")

   print(f"Capital of {x} is {d[x]}")

else:

   print("Not Found.")

Note: d.keys() returns an object that contains all the keys of the dictionary. This function is used so as to access the list of keys.

Refer to the attachment for output.

Attachments:
Answered by viji18net
0

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