193,487 views
40 votes
40 votes
Write a program that asks the user to enter an international dialing code and then looks it up in the country_codes array (see Sec 16.3 in C textbook). If it finds the code, the program should display the name of the corresponding country; if not, the program should print an error message. For demonstration purposes have at least 20 countries in your list.

User Richard Eng
by
2.8k points

1 Answer

14 votes
14 votes

Answer:

Step-by-step explanation:

The following code is written in Python, it prompts the user for a country code, look it up and if found prints out the corresponding country name. If not it prints out an error message stating "Code not found"

country_codes = {"Argentina": 54, "Bangladesh": 880,

"Brazil": 55, "Burma (Myanmar)": 95,

"China": 86, "Colombia": 57,

"Congo: Dem. Rep. of": 243, "Egypt": 20,

"Ethiopia": 251, "France": 33,

"Germany": 49, "India": 91,

"Indonesia": 62, "Iran": 98,

"Italy": 39, "Japan": 81,

"Mexico": 52, "Nigeria": 234,

"Pakistan": 92, "Philippines": 63,

"Poland": 48, "Russia": 7,

"South Africa": 27, "South Korea": 82,

"Spain": 34, "Sudan": 249,

"Thailand": 66, "Turkey": 90,

"Ukraine": 380, "United Kingdom": 44,

"United States": 1, "Vietnam": 84}

user_code = int(input("Enter Country code: "))

keys = list(country_codes.keys())

vals = list(country_codes.values())

if user_code in vals:

print(keys[vals.index(user_code)])

else:

print("Code not found")

User Samiksha
by
2.4k points