So the practice project is as follows:
Say you have a list value like this: spam = ['apples', 'bananas', 'tofu', 'cats']
Write a function 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. For example, passing the previous spam
list to the function would return 'apples, bananas, tofu, and cats'
. But your function should be able to work with any list value passed to it.
So far I've come up with this:
spam = ['apples', 'bananas', 'tofu', 'cats']def commacode(a_list): a_list.insert(-1, 'and') print(a_list)commacode(spam)
And of course the output is just the list values. I've tried to make line 5 as print(str(a_list))
, but that gives a syntax error. My line of thinking is that I have to change it to a string, but I'm lost. Am I missing something in the chapter? I've felt like I've gone over it several times. I feel like len(a_list)
should be somewhere in there but that would just give me a value of 5
. Any thoughts, or how I should go about thinking about this would be great help. I always feel like I'm really understanding this stuff and then I get to these practice projects and am ALWAYS confused on what to do. I know the practice projects are going to use some information we've learned in previous chapters and then focus mainly on the chapter we are on. Chapter 4 covers lists, list values, string values, tuples, copy.copy()
, and copy.deepcopy()
just to name a few.
Link - Chapter4