0
Need help for comma code project
Comma Code 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. my code in comment
5 Answers
+ 2
def myFunc(list):
last = list.pop()
string = list.join(", ")
final = string + "and " + last
return final
+ 2
.pop() is function that takes list, removes last item from it and stores it to a variable.
ie:
list = [1, 2, 3, 4]
last = list.pop()
print(list)
print(last)
will print:
1, 2, 3
4
+ 1
can you please explain the role of list.pop
+ 1
ok..thank you
0
my code
#Comma_code
def comma(given):
mylist=given[0]
while i in range(1,len(given)-1):
mylist+=','+given[i]
while i==len(given)-1:
mylist+='and'+given[i]
print(mylist)
yours=input('enter any list')
comma(list)