Files

20 lines
367 B
Python
Raw Permalink Normal View History

2017-06-12 19:39:10 +08:00
#! python3
'''test for list_to_str'''
2017-04-19 18:26:44 +08:00
2017-06-12 19:39:10 +08:00
SPAM = ['apple', 'bananas', 'tofu', 'cats']
2017-06-14 16:13:51 +08:00
2017-06-12 19:39:10 +08:00
def list_to_str(lst):
'''show list in string format'''
if not lst:
2017-04-19 18:26:44 +08:00
return ''
2017-06-12 19:39:10 +08:00
if len(lst) == 1:
return lst[0]
ret = ''
for i in range(len(lst) - 1):
ret += lst[i] + ', '
ret += 'and ' + lst[-1]
return ret
2017-04-19 18:26:44 +08:00
2017-06-12 19:39:10 +08:00
print(list_to_str(SPAM))