Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何仅使用一个字典并输出与密钥对相关的第三个值?_Python_Python 3.x - Fatal编程技术网

Python 如何仅使用一个字典并输出与密钥对相关的第三个值?

Python 如何仅使用一个字典并输出与密钥对相关的第三个值?,python,python-3.x,Python,Python 3.x,我的讲师给我布置了一项任务,要我完成几个挑战,帮助我提高对词典及其背后概念的理解。我已经能够很容易地完成第一项任务,但第二项任务我却被卡住了。第一个任务是创建一个“谁是你爸爸?”?程序' 挑战是:编写一个Who's Your Daddy程序,让用户输入一个男性的名字,并生成他父亲的名字。(您可以使用名人、虚构人物甚至历史人物来娱乐。)允许用户添加、替换和删除父子配对 我能够创造这个挑战只有一个问题,我只能取代一个儿子的父亲,而不是两者兼而有之 第二个挑战是:增加一个选项,让用户输入一个名字并取回

我的讲师给我布置了一项任务,要我完成几个挑战,帮助我提高对词典及其背后概念的理解。我已经能够很容易地完成第一项任务,但第二项任务我却被卡住了。第一个任务是创建一个“谁是你爸爸?”?程序'

挑战是:编写一个Who's Your Daddy程序,让用户输入一个男性的名字,并生成他父亲的名字。(您可以使用名人、虚构人物甚至历史人物来娱乐。)允许用户添加、替换和删除父子配对

我能够创造这个挑战只有一个问题,我只能取代一个儿子的父亲,而不是两者兼而有之

第二个挑战是:增加一个选项,让用户输入一个名字并取回一个祖父,从而改进这个程序。您的程序仍应仅使用一本子-父对词典。确保在字典中包含几代,以便找到匹配项

我想我可以使用嵌套在词典中的词典并对此进行研究,但它只说明了一本词典。然后我想我可以在字典中使用一个元组,然后在用户请求一个儿子和他们的祖父时访问这个元组,但是也没有太多的运气,所以我决定来这里

所以现在我想知道,我怎样才能将祖父添加到每一对中,你能为字典中的一个键添加第二个值吗


names = { "henry" : "michael",
          "louis" : "jason",
          "owen"  : "justin",
          "jake" : "glynn",
          "adam" : "mark",
          }

choice = None

while choice != "0":

    print(
        """
        Welcome to the Who's Your Daddy? program.
        You can find out a father of the names listed,
        you can then add, replace and delete a son/father pair.
        0 - Exit
        1 - List sons and their fathers
        2 - Add a pair
        3 - Replace a pair
        4 - Delete a pair
        """
        )

    choice = input("Choice: ")

    #exit
    if choice == "0":
        print("Goodbye")

    #List sons
    if choice == "1":
        print(*names.keys(), sep='\n')   
        son_choice = input("Who's father would you like to know the name of?: ")
        if son_choice in names:
            print(names[son_choice])
        else:
            print("That name is not in the list!")

    #Add a pair
    elif choice == "2":
        son_add = input("Enter the name of someone: ").lower()
        if son_add not in names:
            father_add = input("Now enter the name of their father: ").lower()
            names[son_add] = father_add
            print("That pair has been added")
        else:
            print("That name already exists!")


    #Replace a pair
    elif choice == "3":
        son_replace = input("What name do you want to replace?: ")
        if son_replace in names:
            father_replace = input("Enter the name of their father: ")
            names[son_replace] = father_replace
            print("The pair has been replaced")
        else:
            print("That name doesn't exist, please add it first!")

    #Delete a pair
    elif choice == "4":
        son_delete = input("What name do you want me to delete?: ")
        if son_delete in names:
            del names[son_delete]
            print("Pair deleted!")
        else:
            print("That pair does not exist!")

    else:
        print("Sorry, that's an invalid choice!")

input("\n\nPress the enter key to exit!")





在罗纳德和一些常识的帮助下,我现在解决了这个问题

为了找到一位祖父,我需要创建一对新的父子组合,在这个组合中,儿子的父亲将是已经在列表中的儿子。例如,如果我创造一对新的,儿子叫汤姆,父亲叫亨利。然后我可以使用find the grander函数来显示tom的祖父是michael

   #Find a grandfather
    elif choice == "5":
        grandson = input("Please enter the son and I will find the grandfather: ").lower()

        if grandson in names:
            father = names[grandson]
            if father in names:
                grandfather = names[father]
                print("The grandfather of " + grandson + " is " + grandfather)
            else:
                print(father + " is not in the dictionary")

        else:
            print(grandson + " is not in the dictionary")

这是我创建的代码,但是,我将如何使这项工作持续几代,或者这段代码已经完成了这项工作。

如果键是儿子,值是父亲,首先找到父亲,然后再次查找该父亲的名字(值)。祖父就是父亲的父亲。好的,我有点理解你的意思,我知道祖父只是父亲的父亲,我不知道再检查一遍钥匙会如何找到他们的祖父,我将如何实现这一点?给定一个儿子
儿子的选择
名字[儿子的选择]
将是儿子的父亲,对吗?那么谁是名字[son_choice]
的父亲呢?好吧,我理解这一点,但我如何才能为名字[son_choice]创造一个父亲,然后他将成为儿子的祖父呢?我还更新了字典,在原有儿子的基础上有更大和更年轻的一代,我相信这回答了我自己的问题。