Currently working my way through this beginners book and have completed one of the practice projects \'Comma Code\' which asks the user to construct a program which:
As the function must work for all list values passed to it, including integers, therefore it should be able to return/print all values i.e. as str(). My fully working code looks like this:
spam = ['apples', 'bananas', 'tofu', 'cats', 2]
def commacode(words):
x = len(words)
if x == 1:
print(str(words[0]))
else:
for i in range(x - 1):
print((str(words[i]) + ','), end=' ')
print(('and ' + str(words[-1])))
commacode(spam)