[Python] How to print elements of a dictionary that correspond with user input

Discussão em 'Python' iniciado por Stack, Maio 23, 2025.

  1. Stack

    Stack Membro Participativo

    In this problem, I have file.txt that looks like this:

    Tanzania:2948
    Lesotho:2932
    Comoros:2751
    Nepal:2702
    Yemen:2606
    Zimbabwe:2434
    Solomon Islands:2427
    Benin:2276
    Guinea:2247
    Mali:2218
    Kiribati:2185
    Rwanda:2043
    Afghanistan:1976
    Chad:1945
    Ethiopia:1903
    Uganda:1868
    Burkina Faso:1866


    etc.

    My current code looks like this:

    def makeDicts():
    incomeDict = dict()
    countryDict = dict()

    countryList =[]
    gdpList = []
    initialList = []

    with open('lab8.txt','r') as f:
    for line in f:
    line= line.upper().strip().split(':')
    countryList.append(line[0])
    gdpList.append(line[1])
    initial =line[0][0]
    initialList.append(initial)

    for i in range(len(countryList)):
    incomeDict[countryList] = gdpList

    if initialList not in countryDict:
    countryDict[initialList] = set()
    countryDict[initialList].add( countryList)

    return (incomeDict,countryDict)

    userinp = input('Enter File Name: ')
    incomeDict , countryDict = makeDicts()

    while True:
    x = input("Enter a country name or letter or DONE to quit: ")
    x = x.upper()

    if x=='DONE':
    exit()


    I know the function is made correctly as it was made based on exact instructions but I need to make the lower program which I am having trouble with these are the tasks.

    For this exercise, we will write a program that makes use of two dictionaries. The program will be able to do two different functions:

    1. If a country name is entered, the program should report the per capita gross domestic product (GDP) of that country.
    2. If a letter is entered, the program should report all the countries that it has in the system that start with that letter.

    Finally, the specifications ask us to prompt the user for either an initial which returns the set of countries that start with that initial, or a country name that returns the corresponding income, or the word “DONE” which exits the program. We can accomplish this using a while loop that will run until the user enters “DONE”. It will check if the user entered text is in any of the dictionaries and return the appropriate value at that key. Write this into a main() function, with the following suggestions:


    1. First, to construct the dictionaries, write a line of code like incomeDict , countryDict = makeDicts()


    2. Write a while loop and repeated prompt the user for input.


    3. After converting the input to upper-case, you should test whether the user typed in a country name or an initial. These can both be accomplished by testing whether the user input is a key in one of the dictionaries. For instance, if the user input is in a variable called x, then ‘x in countryDict’ will test if the user typed in a letter that is the start of one or more country names.


    4. If the user types in a letter, report all of the countries that start with that letter: These countries start with F: {'FINLAND', 'FIJI', 'FRANCE'}


    5. If the user types in a country name, report the per capita GDP of that country: FINLAND has per capita GDP of 46344


    6. If the user types in DONE, then quit the program.


    7. For all other inputs, report an error and prompt again.

    can you please help me finish this

    I have tried the code that I wrote above but I do not know how to scan the dictionaries and produce the output based on the instructions.

    I do not know what type of functions or operators I can use?

    Continue reading...

Compartilhe esta Página