Итак, практический проект выглядит следующим образом:
Предположим, у вас есть такое значение списка:
Код: Выделить всё
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
list to the function would return
. 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
, 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
should be somewhere in there but that would just give me a value of
. 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,
, and
just to name a few.
Link - Chapter4
Источник:
https://stackoverflow.com/questions/361 ... comma-code