Ask Question
28 August, 11:35

Write a program that reads the data from rawdata_2004. txt into a dictionary whose keys are country names and whose values are per capita incomes. Then the program should prompt the user to enter country names and print the corresponding values. Stop when the user enters quit.

+1
Answers (1)
  1. 28 August, 12:57
    0
    import re

    def country_capita ():

    #opens file

    file=open ("inputfile. txt", "r")

    dictionary=dict ()

    #reads line by line

    for line in file. readlines ():

    # substitutes for multiple space a single space

    line = re. sub (r"[/s]{2, }", ' ', line)

    list=[]

    #splits line on space

    list=line. split (" ")

    #put into dictionary

    dictionary[list[1]]=list[2]

    # get input

    while True:

    choice=input ("Enter the country name or quit to exit: ")

    if choice=="quit":

    break

    elif choice in dictionary. keys ():

    print (dictionary[choice])

    else:

    print ("Invalid Country")

    country_capita ()

    Explanation:

    Using python to code this program. First we, (line 1) import the re module which offers us a set of functions that allows us to search a string for a match. ((line 2) Next we define a function country_capita () that will open rawdata_2004. txt, read the information within line by line and create a dictionary list. The we create a while loop with conditional statements that prompt the user to enter country names, print the corresponding values and stops when the user enters quit.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program that reads the data from rawdata_2004. txt into a dictionary whose keys are country names and whose values are per capita ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers