Question

In: Computer Science

IN PYTHON: Write a program to study countries and their capitals. The program will store country:capital...

IN PYTHON:

Write a program to study countries and their capitals. The program will store country:capital information and allow the user to quiz themselves.

These two functions are required for this assignment. You may add other functions that you deem appropriate:

- menu()   This function, which displays all the user options to the screen, prompts the user for their choice and returns their choice. This function will verify user input and ALWAYS return a valid choice.

- addCapital(dict) This function accepts a dictionary as a parameter. This dictionary contains store country:capital pairs.

THE PROGRAM MUST:

- Add a country and it’s capital to the dictionary. The addCapital function is to be called to complete this task.

- The user can enter a country, and if that country is in the dictionary, it’s capital is printed, otherwise, an error message is printed.

- Print out the number of countries currently represented in the dictionary.

- Print out all the countries which are currently represented in the dictionary.

- Print out all of the countries and their capitals currently stored. If there are no capitals currently stored in the dictionary, a message is printed

- Exit the program

Solutions

Expert Solution


"""
  Python program country capital
"""

def addCaptial(country_capital):
  state = input("Enter Country: ")
  capital = input("Enter Capital: ")
  country_capital[state] = capital
  return country_capital


def getCapital(country_capital):
  state = input("Enter Country: ")
  try:
    return country_capital[state]
  except KeyError:
    print("Error: Country not found")


def printCountries(country_capital):
  for key in country_capital.keys():
    print(key)


def printCountriesAndCapitals(country_capital):
  for key, val in country_capital.items():
    print(key,':',val)


def menu():
  print("1. Add")
  print("2. Find Capital")
  print("3. Number of Countries")
  print("4. All the Countries")
  print("5. All the Countries and their Capitals")
  print("6. Exit")
  choice = int(input("Enter choice: "))
  return choice



if __name__ == '__main__':
  
  country_capital = {
    'Finland': 'Helsinki',
    'France':  'Paris',
    'Germany':  'Berlin'
  }

  while True:
    choice = menu()
    if choice == 1:
      addCaptial(country_capital)
    elif choice == 2:
      getCapital(country_capital)
    elif choice == 3:
      print("Number of countires", len(country_capital))
    elif choice == 4:
      printCountries(country_capital)
    elif choice == 5:
      printCountriesAndCapitals(country_capital)
    elif choice == 6:
      exit()
    print()


Related Solutions

Rewrite Program to store the pairs of states and capitals so that the questions are displayed...
Rewrite Program to store the pairs of states and capitals so that the questions are displayed randomly. import java.util.*; public class quiz { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String arr[][]= {{"Alabama","Montgomery"},{"Alaska","Juneau"},{"Arizona","Phoenix"}}; int n =arr.length; int count=0; for(int i=0;i<n;i++) { System.out.printf("What is the capital of %s? ",arr[i][0]); String capital=sc.next(); if (arr[i][1].equalsIgnoreCase(capital)) { count++; System.out.println("Your answer is correct"); } else { System.out.printf("The correct answer should be %s\n",arr[i][1]); } } System.out.printf("The correct count is %d",count); } }
use python 1. Write a program that a. defines a list of countries that are members...
use python 1. Write a program that a. defines a list of countries that are members of BRICS (Brazil, Russia, India, China, Sri Lanka) b. Check whether a country is a member of BRICS or not Program run Enter the name of country: Pakistan Pakistan is not a member of BRICS Enter the name of country : India India is a member of BRICS 2. Write a program to create a list of numbers in the range of 1 to...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user to enter a capital for a state (by getting a state/capital pair via the StateCapitals class. Upon receiving the user’s input, the program reports whether the answer is correct. The program should randomly select 10 out of the 50 states. Modify your program so that it is guaranteed your program never asks the same state within one round of 10 guesses.
Program must be in Python Write a program in Python whose inputs are three integers, and...
Program must be in Python Write a program in Python whose inputs are three integers, and whose output is the smallest of the three values. Input is 7 15 3
Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
For Python: In this assignment you are asked to write a Python program to determine the...
For Python: In this assignment you are asked to write a Python program to determine the Academic Standing of a studentbased on their CGPA. The program should do the following: Prompt the user to enter his name. Prompt the user to enter his major. Prompt the user to enter grades for 3 subjects (A, B, C, D, F). Calculate the CGPA of the student. To calculate CGPA use the formula: CGPA = (quality points * credit hours) / credit hours...
In python. Projectile motion: Write a python program that will ask the user for      an...
In python. Projectile motion: Write a python program that will ask the user for      an initial height y0, initial velocity v, launch angle theta, and mass m.      Create two functions, one that will calculate max height      of the projectile, and one that will calculate the range. Ask the     user which one he/she would like to calculate, then present them with the answer. (use kg, m and m/s)
Please write in beginner level PYTHON code! Your job is to write a Python program that...
Please write in beginner level PYTHON code! Your job is to write a Python program that asks the user to make one of two choices: destruct or construct. - If the user chooses to destruct, prompt them for an alternade, and then output the 2 words from that alternade. - If the user chooses construct, prompt them for 2 words, and then output the alternade that would have produced those words. - You must enforce that the users enter real...
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the...
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the equivalent radix-e (another arbitrary base) number. Where e and d are members in [2, 16]. Remember, base 16 needs to be calculated as hexadecimal. So, if radix-d is input as a hexadecimal number, it needs to convert and output to desired base. Conversely, if base 16 is the desired output, then the output needs to show a hexadecimal number. Hints: The easiest approach is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT