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

5th Nov 2016, 3:30 PM
harjeevan singh
harjeevan singh - avatar
5 Respuestas
+ 2
def myFunc(list): last = list.pop() string = list.join(", ") final = string + "and " + last return final
5th Nov 2016, 4:39 PM
Redstone Tehnik
Redstone Tehnik - avatar
+ 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
5th Nov 2016, 4:47 PM
Redstone Tehnik
Redstone Tehnik - avatar
+ 1
can you please explain the role of list.pop
5th Nov 2016, 4:43 PM
harjeevan singh
harjeevan singh - avatar
+ 1
ok..thank you
5th Nov 2016, 6:47 PM
harjeevan singh
harjeevan singh - avatar
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)
5th Nov 2016, 3:30 PM
harjeevan singh
harjeevan singh - avatar