I am trying to generate a list python. In this program i am trying to write a function that takes a list value from keyboard 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'. Her is my code
spam=[]
def rock(val):
while True:
print("Enter the text: ")
val=input()
spam.append(val)
return spam
print("Enter the number: ")
n=int(input())
for i in range(0,n):
s=', '.join(rock(''))
print(s)
Output:
Enter the number:
3
Enter the text:
rock
rock
Enter the text:
dog
rock, dog
Enter the text:
cad
rock, dog, cad
Now, in the above mention program i have succeed to generate a list which is separated by comma. But i can't figure out how to put "and" just before the last value. like this, 'apples, bananas, tofu, and cats'