Python 为什么不是';我的函数是否在字典中追加键值?

Python 为什么不是';我的函数是否在字典中追加键值?,python,dictionary,Python,Dictionary,所以我定义了这个函数,它询问你一个国家的名称,然后给你一个有趣的事实。它应该问你是否想把你问到的国家添加到字典中(如果它还不知道的话)。我认为代码很好。但在它问你:/有人能找出问题所在吗?/之后,它并没有真正将新的国家事实添加到字典中 def countries(): param = input("please enter the country you'd like a fun fact about") listofcountries = {"Kuwait": "has 10%

所以我定义了这个函数,它询问你一个国家的名称,然后给你一个有趣的事实。它应该问你是否想把你问到的国家添加到字典中(如果它还不知道的话)。我认为代码很好。但在它问你:/有人能找出问题所在吗?/之后,它并没有真正将新的国家事实添加到字典中

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.")

listofcountries
是一个局部变量,因此每次调用函数时都会重置。如果希望它在调用之间保持其值,则需要将其设置为全局(或其他更高的范围)

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.")
还要注意,我将
input
的所有实例都更改为
raw\u input
。您想要读取字符串,因此您肯定想要
原始输入
input
函数将实际计算输入,将其转换为符号或原始文字值


根据下面的评论,我回到了
input
,因为这显然是Python3中正确的函数。

嗯,
listofcountries
是函数
countries()中的一个局部变量。当函数返回时,您的字典将消失!以及所有其他局部变量(如
param
add_yes

最好将
国家列表
定义为函数外部的一个全局模块。然后,
listofcountries
将在调用
国家时保留其值


更高级的是创建一个类,并将dict存储为该类的属性。但是,除此之外,将其作为一个全球模块应该可以很快解决您的问题。

国家列表是本地的,每次都会重置。您可以使用具有默认值的参数跨调用持久化,这允许您缓存任何结果,而不会污染全局命名空间:

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)
    ...

它确实为你的字典增添了价值。但事后你不用字典。如果再次调用
countries()
,将再次设置
listofcountries
字典(代码的第3行)。此外,它是一个局部变量,因此在函数外部找不到它。将
input
更改为
raw\u input
可能是一个错误。
print
调用中的括号表示Python 3。@user2357112-谢谢,我更新了答案。我没有使用Python3,所以我没有意识到这一点。