-2

I am trying to create a Dictionary of code and their country, Example for my dictionary:

capital_dict = {'My':'Malaysia','In':'India','Si':'Singapore','Vi':'Vietname'}.

Whenever the user enter the correct code like 'My', the program will print out 'The Code is My and the country is Malaysia'

also if the user enter an invalid code, the program will print out 'Country is not in the list'.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Rusyaidi
  • 1
  • 1

1 Answers1

3

Can be done using get method. The first parameter of get method specifies key and second parameter is returned if key is not present in dictionary.

print(capital_dict.get("My","Country is not in the list"))
Ashish M J
  • 642
  • 1
  • 5
  • 11