I got a little inspired by atraubs 'spot the error' topic in the advanced discussion forum. So here is a little bug finding problem for intermediate python programmers (experts please keep your tongue in check). The first and second print statement show the expected result. The third does not! Can you explain the bug?
def append_u_words(words, return_list=[]):
"""Supposed to return a list where all elements in words starting with 'u'
have been added to return_list. And if return_list is not given as an
argument returns a list of the strings in words starting 'u'.
"""
u_words = [word for word in words if word.startswith('u')]
return_list += u_words
return return_list
print append_u_words(["user", "tomorrow"]) #should print ['user']
print append_u_words(["unity", "yabbayabba"], ["still_works"]) #should print ['still_works', 'unity']
print append_u_words(["uncool"]) #should print ['uncool'] , but does something else

New Topic/Question
Reply



MultiQuote






|