绝对初学者Python编程第5章挑战4

绝对初学者Python编程第5章挑战4,python,python-3.x,Python,Python 3.x,挑战是改进之前的一个挑战,谁是你的爸爸看我的成功代码:通过添加一个选项,让用户输入一个名字,并取回一个祖父。程序仍然应该只使用一个子-父对字典 我不能让它工作。到目前为止,我的全部代码可在以下网址查看: 很明显,我让这条路变得更加困难,现在我陷入了一个复杂的世界。这是我编好的代码: # create dictionary paternal_pairs ={"a": "b", "b": "c", "c": "d"} # init

挑战是改进之前的一个挑战,谁是你的爸爸看我的成功代码:通过添加一个选项,让用户输入一个名字,并取回一个祖父。程序仍然应该只使用一个子-父对字典

我不能让它工作。到目前为止,我的全部代码可在以下网址查看:

很明显,我让这条路变得更加困难,现在我陷入了一个复杂的世界。这是我编好的代码:

# create dictionary
paternal_pairs ={"a": "b",
                 "b": "c",
                 "c": "d"}

# initialize variables
choice = None

# program's user interface
while choice != 0:
print(
"""
   Who's Yo Daddy:

   2 - Look Up Grandfather of a Son
   """
)

choice = input("What would you like to do?: ")
print() 

    # look up grandfather of a son
    if choice == "2":
        son = input("What is the son's name?: ")
        # verify input exists in dictionary
        if son in paternal_pairs.values():
            for dad, boy in paternal_pairs.items():
                if dad == son:
                    temp_son = dad
                    for ol_man, kid in paternal_pairs.items():
                        if temp_son == kid:
                            print("\nThe grandfather of", son, "is", ol_man)
                        else:
                            print("\nNo grandfather listed for", son)
                else:
                    print("\nNo grandfather listed for", son)
        # if input does not exist in dictionary:
        else:
            print("Sorry, that son is not listed. Try adding a father-son pair.")
选择2后,我的输出:

What is the son's name?: d

No grandfather listed for d

No grandfather listed for d

No grandfather listed for d

No grandfather listed for d

No grandfather listed for d

No grandfather listed for d

No grandfather listed for d

No grandfather listed for d

显然,它暂时被困在一个小循环中,不起作用。所有其他代码都按预期工作。救命啊

循环dict中的每个条目,并匹配值,如果不匹配,则为每个键值对打印不匹配的内容

它相当于以下简化循环:

>>> for i in range(3):
...     if i == 5:
...         print(i)
...     else:
...         print('Not 5')
... 
Not 5
Not 5
Not 5
改为使用,只有在完成所有值的检查后才会调用它;如果找到匹配项,请使用中断:

for ol_man, kid in paternal_pairs.items():
    if temp_son == kid:
        print("\nThe grandfather of", son, "is", ol_man)
        break
else:
    print("\nNo grandfather listed for", son)
else:子句与for循环一起使用时如何工作的小演示:


在第一个示例中,我们用一个break来中断循环,但在第二个示例中,我们从未达到break语句i从不等于5,因此将达到else:子句,并将其打印出来。

您循环dict中的每个条目,并匹配该值,如果不匹配,然后,对于打印的每个键值对,它都不匹配

它相当于以下简化循环:

>>> for i in range(3):
...     if i == 5:
...         print(i)
...     else:
...         print('Not 5')
... 
Not 5
Not 5
Not 5
改为使用,只有在完成所有值的检查后才会调用它;如果找到匹配项,请使用中断:

for ol_man, kid in paternal_pairs.items():
    if temp_son == kid:
        print("\nThe grandfather of", son, "is", ol_man)
        break
else:
    print("\nNo grandfather listed for", son)
else:子句与for循环一起使用时如何工作的小演示:


在第一个例子中,我们用一个break来中断循环,但在第二个例子中,我们从未达到break语句i从不等于5,所以else:子句被达到并被打印出来。

@hamza:没有错误,我想,只是dict中每个条目的d行都列出了一个No-grander…@hamza:没有错误,我想,只是一个没有祖父列出的d行为每个条目在dict。。。