Abdul
Abdul

Reputation: 53

Why isn't my function appending a key-value to the dictionary?

So I defined this function that queries you about the name of a country and then gives you a fun fact about it. It's supposed to ask you if you want to add the country you asked about to the dictionary (if it doesn't already know it). I think the code is just fine. But it doesn't really add the new country-fact to the dictionary after it asks you :/ Can someone please identify the problem?

def countries():
    param = input("please enter the country you'd like a fun fact about")
    listofcountries = {"Kuwait": "has 10% of the world's oil reserves.", "UAE": "has the world's highest free-standing structrure, Burj Khalifa.", "Qatar": "is hosting the FIFA 2022 world cup.", "Saudi Arabia": "is the largest GCC country.", "Bahrain": "held the Dilmun civilization, one of the world's oldest civilizations.", "Oman": "is known for its beautiful green mountains."}
    if (param in listofcountries):
        print ("I know something about", param)
        print (param, listofcountries[param])
    else:
        print ("I don't know anything about", param)
        add_yesorno = input("Would you like to add something to the list? (yes or no)")
        if add_yesorno == 'yes':
            country_name = input("What's its name again?")
            add_yes = input("please finish this sentence. This country...")
            print ("Thanks a lot for contributing to the list!")
            listofcountries[country_name] = add_yes
        else:
            print ("Thanks then! See you later.")

Upvotes: 0

Views: 100

Answers (3)

DaoWen
DaoWen

Reputation: 33019

listofcountries is a local variable, and as such will be reset every time you call the function. You need to make it global (or some other higher scope) if you want it to persist its value between calls.

listofcountries = {"Kuwait": "has 10% of the world's oil reserves.", "UAE": "has the world's highest free-standing structrure, Burj Khalifa.", "Qatar": "is hosting the FIFA 2022 world cup.", "Saudi Arabia": "is the largest GCC country.", "Bahrain": "held the Dilmun civilization, one of the world's oldest civilizations.", "Oman": "is known for its beautiful green mountains."}

def countries():
    param = input("please enter the country you'd like a fun fact about")
    if (param in listofcountries):
        print ("I know something about", param)
        print (param, listofcountries[param])
    else:
        print ("I don't know anything about", param)
        add_yesorno = input("Would you like to add something to the list? (yes or no)")
        if add_yesorno == 'yes':
            country_name = input("What's its name again?")
            add_yes = input("please finish this sentence. This country...")
            print ("Thanks a lot for contributing to the list!")
            listofcountries[country_name] = add_yes
        else:
            print ("Thanks then! See you later.")

Also note that I changed all instances of input to raw_input. You want to read in a string, so you definitely want raw_input. The input function will actually evaluate the input, turning it into a symbol or a raw literal value.

As per the comment below, I've reverted to input since apparently that's the correct function in Python 3.

Upvotes: 1

AChampion
AChampion

Reputation: 30268

listofcountries is local and gets reset each time. You can use an argument with a default value to persist across calls, which allows you to cache any results without polluting the global namespace:

def countries(listofcountries = {}):
    defaultlist = {"Kuwait": "has 10% of the world's oil reserves.", "UAE": "has the world's highest free-standing structrure, Burj Khalifa.", "Qatar": "is hosting the FIFA 2022 world cup.", "Saudi Arabia": "is the largest GCC country.", "Bahrain": "held the Dilmun civilization, one of the world's oldest civilizations.", "Oman": "is known for its beautiful green mountains."} 
    if not listofcountries:
        listofcountries.update(defaultlist)
    ...

Upvotes: 0

Tim Peters
Tim Peters

Reputation: 70602

Well, listofcountries is a local variable in function countries(). When the function returns, your dictionary disappears! Along with all the other local variables (like param and add_yes).

Better to define listofcountries outside the function, as a module global. Then listofcountries will retain its value across calls to countries.

More advanced is to create a class, and store the dict as an attribute of the class. But, short of that, a making it a module global should fix your problem quickly.

Upvotes: 0

Related Questions