Question: Write a Python program (in python 3) that will read in the countries and their capitals from the countryCap.txt file. Look at the file. It
Write a Python program (in python 3) that will read in the countries and their capitals from the countryCap.txt file. Look at the file. It is a comma-delimited file (comma between country and capital). You will need to get a line from the .txt file, split the line into a country and a capital (remember that the split method returns a list) and then store them in a dictionary. (As you recall from Chapter 8, a dictionary has a key/value pair. The key can be the country name, and the value can be the capital name.) Once all data from the text file has been stored in the dictionary, allow the user to enter the name of a country, then find the capital and display it to the user. Loop back up and let them enter another country until they enter a value of x to stop the loop.
Make sure to handle the following exceptions if the users country name is not found in the dictionary, or if the countryCap.txt file is not found when you try to open it.
CountryCap.txt:
Afghanistan,Kabul Albania,Tirana Andorra,Andorra la Vella Angola,Luanda Antigua and Barbuda,St. Johns Argentina,Buenos Aires Armenia,Yerevan Australia,Canberra Austria,Vienna Azerbaijan,Baku Bahamas,Nassau Bahrain,Manama Bangladesh,Dhaka Barbados,Bridgetown Belarus,Minsk Belgium,Brussels Belize,Belmopan Bhutan,Thimphu Bolivia,La Paz and Sucre Bosnia,Sarajevo Botswana,Gaborone Brazil,Braslia Brunei,Bandar Seri Begawan Bulgaria,Sofia Burkina Faso,Ouagadougou Burundi,Bujumbura Cambodia,Phnom Penh Cameroon,Yaound Canada,Ottawa Cape Verde,Praia Cayman Islands,George Town Central African Republic,Bangui Chad,NDjamena Chile,Santiago China,Beijing Colombia,Bogot Comoros,Moroni Costa Rica,San Jos Cte dIvoire,Yamoussoukro Croatia,Zagreb Cuba,Havana Cyprus,Nicosia Czech Republic,Prague Democratic Republic of the Congo,Kinshasa Denmark,Copenhagen Djibouti,Djibouti Dominica,Roseau Dominican Republic,Santo Domingo East Timor,Dili Ecuador,Quito Egypt,Cairo El Salvador,San Salvador Equatorial Guinea,Malabo Eritrea,Asmara Estonia,Tallinn Ethiopia,Addis Ababa Fiji,Suva Finland,Helsinki France,Paris French Guiana,Cayenne Gabon,Libreville Georgia,Tbilisi Germany,Berlin Ghana,Accra Greece,Athens Grenada,St Georges Guatemala,Guatemala Guinea,Conakry Guyana,Georgetown Haiti,Port-au-Prince Honduras,Tegucigalpa Hungary,Budapest Iceland,Reykjavik India,New Delhi Indonesia,Jakarta Iran,Tehran Iraq,Baghdad Ireland,Dublin Israel,Jerusalem Italy,Rome Jamaica,Kingston Japan,Tokyo Jordan,Amman Kazakhstan,Astana Kenya,Nairobi Kiribati,South Tarawa Kuwait,Kuwait Kyrgyzstan,Bishkek Laos,Vientiane Latvia,Riga Lebanon,Beirut Lesotho,Maseru Liberia,Monrovia Libya,Tripoli Liechtenstein,Vaduz Lithuania,Vilnius Luxembourg,Luxembourg City Madagascar,Antananarivo Malawi,Lilongwe Malaysia,Kuala Lumpur Maldives,Mal Mali,Bamako Malta,Valletta Marshall Islands,Majuro Mauritania,Nouakchott Mauritius,Port Louis Mexico,Mexico City Micronesia,Palikir Moldova,Chisinau Monaco,Monaco Mongolia,Ulaanbaatar Montenegro,Podgorica Morocco,Rabat Mozambique,Maputo Myanmar,Naypyidaw Namibia,Windhoek Nauru,Yaren Nepal,Kathmandu Netherlands,Amsterdam New Zealand,Wellington Nicaragua,Managua Niger,Niamey Nigeria,Abuja North Korea,Pyongyang Norway,Oslo Oman,Muscat Pakistan,Islamabad Palau,Koror Palestine,Jerusalem Panama,Panama City Papua New Guinea,Port Moresby Paraguay,Asuncion Peru,Lima Philippines,Manila Poland,Warsaw Portugal,Lisbon Puerto Rico,San Juan Qatar,Doha Republic of Macedonia,Skopje Republic of the Congo,Brazzaville Romania,Bucharest Russia,Moscow Rwanda,Kigali Saint Kitts and Nevis,Basseterre Saint Lucia,Castries Saint Vincent and the Grenadines,Kingstown Samoa,Apia San Marino,San Marino So Tom and Prncipe,So Tom Saudi Arabia,Riyadh Senegal,Dakar Serbia,Belgrade Seychelles,Victoria Sierra Leone,Freetown Singapore,Singapore Slovakia,Bratislava Slovenia,Ljubljana Solomon Islands,Honiara Somalia,Mogadishu South Africa,Pretoria South Korea,Seoul South Sudan,Juba Spain,Madrid Sudan,Khartoum Suriname,Paramaribo Swaziland,Mbabane Sweden,Stockholm Switzerland,Bern Syria,Damascus Taiwan,Taipei Tajikistan,Dushanbe Thailand,Bangkok Togo,Lome Tonga,Nukualofa Trinidad and Tobago,Port of Spain Tunisia,Tunis Turkey,Ankara Turkmenistan,Asgabat Turks and Caicos,Cockburn Town Tuvalu,Funafuti Uganda,Kampala Ukraine,Kiev United Arab Emirates,Abu Dhabi United Kingdom,London United States,Washington DC Uruguay,Montevideo Uzbekistan,Tashkent Vanuatu,Port Vila Vatican City,Vatican City Venezuela,Caracas Vietnam,Hanoi Western Sahara,Layoun Yemen,Sanaa Zambia,Lusaka Zimbabwe,Harare
You will need to create two functions one called fillDictionary() and one called findCapital(). Pass the appropriate data using a parameter list. Do not use global variables.
The output from your program should look like this

Enter a country (x to stop): Belgium Capital of Belgium is Brussels Enter a country (x to stop) :Ireland Capital of Ireland is Dublin Enter a country (x to stop): Chile Capital of Chile is Santiago Enter a country (x to stop) Puerto Rico Capital of Puerto Rico is San Juan Enter a country (x to stop): perto rica Country not found Enter a country (x to stop):x Goodbye
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
