0

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

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

3 回答 3

1

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

另请注意,我将所有实例更改为inputto raw_input。你想读入一个字符串,所以你肯定想要raw_input. 该input函数将实际评估输入,将其转换为符号或原始文字值。

根据下面的评论,我已经恢复,input因为显然这是 Python 3 中的正确函数。

于 2013-09-18T02:23:47.710 回答
0

listofcountries 是本地的,每次都会重置。您可以使用具有默认值的参数在调用之间保持不变,这允许您缓存任何结果而不会污染全局命名空间:

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)
    ...
于 2013-09-18T02:52:18.800 回答
0

嗯,listofcountries是函数中的局部变量countries()。当函数返回时,你的字典就消失了!连同所有其他局部变量(如paramadd_yes)。

最好在函数listofcountries 外部定义为模块全局。然后listofcountries将在调用countries.

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

于 2013-09-18T02:24:59.840 回答