这是我要求做的:
save_friends(filename, friends_list)
获取文件名和好友列表,并将该列表以正确的格式写入文件。因此,例如, save_friends('friends.csv', load_friends('friends.csv')) 应该用完全相同的内容覆盖friends 文件。
这是我的代码:
def save_friends(filename, friends_list):
"""
take a file name and a friends list and wrients that list
to the file in the correct format
save_friend(file, list) -> list
"""
friends_list = []
f = open(filename, 'w')
for friend in friends_list:
f.write(friend+ '\n')
return friends_list
f.close()
问题是当我运行文本代码(由学校提供的代码进行一些简单测试)时,它告诉我
>>>save_friends('friends_output.csv', d)
[ ]
Traceback (most recent call last):
File "E:\study\Yr 1\Semister1\CSSE1001\Assignment\sample_tests.py", line 84, in <module>
assert res == None, "save_friends didn't return None"
AssertionError: save_friends didn't return None
那么,当输入不是真正的列表时,我怎样才能返回“无”(就像在这种情况下输入“d”)?