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:
I came up with this solution
#This is the list which needs to be converted to String
spam = ['apples', 'bananas', 'tofu', 'cats']
#This is the empty string in which we will append
s = ""
def list_to_string():
global spam,s
for x in range(len(spam)):
if s == "":
s += str(spam[x])
elif x == (len(spam)-1):
s += " and " + str(spam[x])
else:
s += ", " + str(spam[x])
return s
a = list_to_string()
print(a)