擅长:python、mysql、java
<p>问题是,您只遍历最喜爱的\u语言中的项,因此第二个if语句的计算结果永远不会为True</p>
<p>相反,您可以使用set(),它只从常用\u语言中的键和poll \u takers中的值中获取唯一值:</p>
<p>最喜欢的语言={'Jen':'python','Sarah':'c','Edward':'Ruby','Phil':'python'}
poll\u takers=['Jen'、'Greg'、'Sarah'、'Allan'、'Bob']</p>
<pre><code>for name in set(favorite_languages + poll_takers):
try:
language = favorite_languages[name]
except KeyError:
# This means the name isn't in your favorite_languages dictionary
print('Please take our poll!')
return
print(name.title() + "'s favorite language is " + language.title() + ".")
print('Thank you for taking the poll!')
</code></pre>