"""Comma code
This program converts a list to a comma separated string.
Write a function, :meth:`to_string` that takes a list value as an argument and
returns a string with all the items separated by a comma and a space, with `and`
inserted before the last item.
Example:
>>> from pythontutorials.books.AutomateTheBoringStuff.Ch04.Projects.P01_comma_code import to_string
>>> spam = ['apples', 'bananas', 'tofu', 'cats']
>>> to_string(spam)
'apples, bananas, tofu, and cats'
But your function should be able to work with any list value passed to it.
"""
import copy
[docs]def to_string(input_list: list) -> str:
"""To string
Converts elements in :obj:`list` to comma-separated :obj:`str`.
Args:
input_list: List to convert into a string.
Returns:
String with each element in the list separated by a comma and a
space with `and` inserted before the last element.
"""
temp_list = copy.copy(input_list) #: Don't modify input_list
temp_list.insert(-1, "and ")
for index in range(0, len(temp_list) - 2):
temp_list[index] += ', '
return ''.join(temp_list)
[docs]def main():
spam = ['apples', 'bananas', 'tofu', 'cats']
print(to_string(spam))
spam = ['lions', 'tigers', 'bears']
print(to_string(spam) + ". Oh, my!")
if __name__ == '__main__':
main()