+ 1
Help explain how to write this Function
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.
12 Answers
+ 2
Why don't you give a try??
+ 1
not just merely printing out the values
+ 1
Thank you Sami Khan
0
spam = ['apples','bananas','tofu','cats']
def listFunction(listValue):
listValue.insert(3, 'and')
return listValue
print(listFunction(spam))
OUTPUT: ['apples', 'bananas', 'tofu', 'and', 'cats']
I donāt think this is right because if the size of the list changes, this funcation will not be effective. commas are not added. Sami Khan
0
But u can get the length of the list right?
Just subtract one from it
listValue.insert(len(spam)-1, 'and')
0
thanks that works now, but how do I put commas in between the list values?
0
Cbrā[ ZeroCharisma ] its not a homework, reading the book: Automate the boring stuff with Python. thats an excercise, thank you
0
This is my result :
def a(spam):
for i in range(len(spam)-1):
if spam[0] in spam[i]:
print("'",end='')
c=(spam[i]+',')
print(c,end='')
if spam[-1] not in spam[i]:
print('and '+spam[-1]+"'")
a(['tĆ”o', 'chuį»i', 'Äįŗu phį»„', 'mĆØo'])
0
def lol(a):
lastWord = a[len(a)-1] + '.'
del a[len(a)-1]
a.append('end ' + lastWord)
L=0
while L<=len(a)-2:
print(a[L],end=', ')
L=L+1
else:
print(a[len(a)-1])
spam = ['apples','bat','tofu','cats']
lol(spam)
0
spam = ['apples','bananas','tofu','cats','apples','bananas','tofu','cats']
def listFunction(listValue):
i = 0
while i < len(listValue)-1:
print (listValue[i]+',', end='')
i=i+1
print('and '+listValue[-1])
listFunction(spam)