我意识到还有其他类似的问题,但我不太明白。
假设有一本字典:
fav_food = {'jen':'pizza','eric':'burrito','jason':'spaghetti','tom':'mac'}
然后有一个列表:
users = ['jason', 'phil', 'jen', 'ben']
这里的场景是
if a user in the list 'users' is in the dict. 'fav_food.keys()',
then print(the user + " likes" + fav_food[the user])
if a user in the list 'users' is not in the dict. 'fav_food.keys()',
then print(the user + " hasn't taken the poll")
回报应该是:
Jason likes Spaghetti
Phil hasn't taken the poll
Jen likes Pizza
Ben hasn't taken the poll
我想使用循环 'for' 并以某种方式通过字典迭代列表......但无论我做什么,我都会收到错误。
如果可能的话,我更愿意以最“Python”的方式来做。
SMILET
开满天机
繁花如伊
相关分类