Hi lets say I have a list containing these string values:
food = ['apples', 'bananas', 'tofu', 'pork']
I'm tasked to write a function that takes the 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
My solution to this was:
def formatList(food):
result = ""
for idx in food:
result += idx + "," + " "
return result
if I print this called function the result is:
print(formatList(food))
>> apples, bananas, tofu, pork,
The intended output is supposed to be:
print(formatList(food))
>> 'apples, bananas, tofu, and pork'
How can i fix this?