Практический проект ATBSWP Глава 4: Код запятойPython

Программы на Python
Ответить Пред. темаСлед. тема
Гость
 Практический проект ATBSWP Глава 4: Код запятой

Сообщение Гость »


Итак, практический проект выглядит следующим образом:

Предположим, у вас есть такое значение списка:

Код: Выделить всё

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

Код: Выделить всё

'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 . 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


Источник: https://stackoverflow.com/questions/361 ... comma-code
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Лучший практический подход: SSM-команды Lambda Python против библиотеки Python-LDAP
    Anonymous » » в форуме Python
    0 Ответы
    10 Просмотры
    Последнее сообщение Anonymous
  • Практический экзамен для инженеров по данным (DE601P) (Datacamp)
    Anonymous » » в форуме Python
    0 Ответы
    22 Просмотры
    Последнее сообщение Anonymous
  • Практический экзамен для инженеров по данным (DE601P) (Datacamp)
    Anonymous » » в форуме Python
    0 Ответы
    15 Просмотры
    Последнее сообщение Anonymous
  • REACT ERROR не может быть дочерним элементом . См. (неизвестно) > глава > th
    Anonymous » » в форуме Jquery
    0 Ответы
    33 Просмотры
    Последнее сообщение Anonymous
  • Автоматизация скучной работы. Глава 6. Настольный принтер почти готов
    Anonymous » » в форуме Python
    0 Ответы
    14 Просмотры
    Последнее сообщение Anonymous

Вернуться в «Python»