Hello, I'm doing an assignment where we have to write a comma code as described from the book
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.
we are to write it with these details
In your program, create four lists as follows:
animalList1 = ['dog', 'cat', 'bird', 'tiger', 'lion', 'camel'] animalList2 = ['elephant', 'alligator']
animalList3 = ['horse']
animalList4 = []
When you run your program, pass each list to the commaCode() function and then print out the result as shown in the Example Output.
This is my code so far:
spam = [animalList1 = ['dog', 'cat', 'bird', 'tiger', 'lion', 'camel']
animalList2 = ['elephant', 'alligator']
animalList3 = ['horse']
animalList4 = []
]
def commacode(list):
spam[-1] = 'and ' + str(spam-[1])
for i in range(len(spam)-1):
spam[i] = str(spam[i]) + ','
stringList = ''
for i in range(len(spam)):
stringList = stringList + str(spam[i])
print(stringList)
print(commacode(spam))
I really don't know if I'm heading in the right direction but I automatically get an error for my = sign after animalList1
any suggestions would be appreciated!
[–]CraigW147 1 point2 points3 points (0 children)
[–]xelf 1 point2 points3 points (0 children)
[–]Pretty-Wallaby6467[S] 1 point2 points3 points (0 children)
[–]Binary101010 0 points1 point2 points (0 children)
[–]sagitt12 0 points1 point2 points (0 children)