11.3 - Countries and capitals

Problem

Write a program that asks the user to enter the names of three countries and the capital city of each.

The countries and capitals will be stored in parallel arrays.

The arrays should be displayed and written to file (countries.csv) as shown.

Enter country 1: Scotland
Enter country 2: England
Enter country 3: Wales

Enter the capital city of Scotland: Edinburgh
Enter the capital city of England: London
Enter the capital city of Wales: Cardiff

Edinburgh is the capital of Scotland.
London is the capital of England.
Cardiff is the capital of Wales.
Scotland,Edinburgh
England,London
Wales,Cardiff

 

1 Get countries from user IN  
OUT countries
2 Get capitals from user IN countries
OUT capitals
3 Display arrays IN countries, capitals
OUT  
4 Write arrays to file IN countries, capitals
OUT  

 

3.1 Open countires.csv
3.2 Repeat 3 times
3.3     Write current country to file
3.4     Write "," to file
3.5     Write current capital to file
3.6     Write "\n" to file
3.7 End repeat
3.8 Close file

 

# Main
countries = get_countries()
capitals = get_capitals(countries)
display_data(countries, capitals)
write_data(countries, capitals)
def get_countries():
    countries = [str] * 3
    for i in range(3):
        countries[i] = input("Enter country " + str(i + 1) + ": ")
    return countries

def get_capitals(countries):
    capitals = [str] * 3
    for i in range(3):
        capitals[i] = input("Enter the capital city of " + countries[i] + ": ")
    return capitals

def display_data(countries, capitals):
    for i in range(3):
        print(capitals[i], "is the capital of", countries[i] + ".")

def write_data(countries, capitals):
    file = open("countries.csv", "w")
    for i in range(3):
        file.write(countries[i] + "," + capitals[i] + "\n")
    file.close()

# Main
countries = get_countries()
capitals = get_capitals(countries)
display_data(countries, capitals)
write_data(countries, capitals)
def get_countries():
    countries = [str] * 3
    for i in range(3):
        countries[i] = input("Enter country " + str(i + 1) + ": ")
    return countries

def get_capitals(countries):
    capitals = [str] * 3
    for i in range(3):
        capitals[i] = input("Enter the capital city of " + countries[i] + ": ")
    return capitals

def display_data(countries, capitals):
    for i in range(3):
        print(capitals[i], "is the capital of", countries[i] + ".")

# write_data
# Writes data from two parallel arrays (countries and capitals) into a CSV file.
# open("countries.csv", "w") – Opens or creates the file for writing. 
# The "w" mode overwrites any existing content.
# The loop repeats 3 times (once for each country and capital).
# Each line is written in the format country,capital followed by "\n" to move to the next line.
# close() – Closes the file and saves all written data.
def write_data(countries, capitals):
    file = open("countries.csv", "w")
    for i in range(3):
        file.write(countries[i] + "," + capitals[i] + "\n")
    file.close()

# Main
countries = get_countries()
capitals = get_capitals(countries)
display_data(countries, capitals)
write_data(countries, capitals)

Extension

Update the program to ask for the population of each city. This data should be displayed and written to file.

Target

Write parallel arrays to file