https://gyazo.com/672475d1961538af601bcfa2781f3ef2 正如你所看到的,「Got」和「Expected」列表是一樣的,但是由於它的對象的順序是隨機的,所以它給了我一個doctest失敗。我的代碼不能改變,所以這種隨機化是不可避免的。我如何解決這個doctest問題。這是不起作用的功能。即使輸出正確,我的doctest也給了我失敗的例子
def all_followers(data_dict, followed_user):
""" {str: dict of {str: object}}, str -> list of str
Returns a list containing the username of all the users in data_dict
that are following followed_user
>>> all_followers(process_data(open("small_data.txt")), "katieH")
['tomCruise']
>>> all_followers(process_data(open("rdata.txt")), "arrington")
['AccordionGuy', 'vkhosla', 'bhorowitz', 'peterfenton', 'mattcohler', 'michaelcvet', 'google', 'KatieS']
"""
# The list to be returned, is created
followers_list = []
for key in data_dict:
# Every username in data_dict and their "following" list is checked to
# see if it matches the username of the followed_user
if followed_user in data_dict[key]["following"]:
# If key follows followed_user, the name of key is appended to the
# followers_list
followers_list.append(key)
return followers_list
請避免鏈接和圖像(尤其是指向圖像的鏈接),而是直接將代碼(以及生成的錯誤消息)粘貼到您的問題中。 – DilithiumMatrix