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,...
A customer comes into a grocery store and buys 8 items. Write a PYTHON program that...
A customer comes into a grocery store and buys 8 items. Write a PYTHON program that asks for the name of the item and the price of each item, and then displays these on the screen. Refer to the print() and input() statements covered in the module. Do NOT use loops as it will be covered in later modules. Apples 2.10 Hamburger 3.25 Milk 3.49 Sugar 1.99 Bread 1.76 Deli Turkey 7.99 Pickles 3.42 Butter 2.79
Python: Write a program to simulate the purchases in a grocery store. A customer may buy...
Python: Write a program to simulate the purchases in a grocery store. A customer may buy any number of items. So, you should use a while loop. Your program should read an item first and then read the price until you enter a terminal value (‘done’) to end the loop. You should add all the prices inside the loop. Add then a sales tax of 8.75% to the total price. Print a receipt to indicate all the details of the...
In Python... Write a program that randomly assign participants for a study (use the random module)....
In Python... Write a program that randomly assign participants for a study (use the random module). The program should: Display a message stating its goal Prompt the user to enter Number of participants in the study (sample size) Minimum age of the participants Maximum age of the participants Based on these specifications, randomly assign for each participant: Gender (you can use "1" for Male and "2" for Female) Age The program should calculate and display: Total number of male participants...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT