Okay so I'm trying to make a function that will take a list of items and return (not print!) a string of that list, separated by commas with an 'and' before the last item in the list. My script so far looks like this:
rose1 = Thing("red rose", "flowerbox")
rose2 = Thing("pink rose","garden")
rose3 = Thing("white rose", "vase")
def text_list(things):
"""Takes a sequence of Things and returns a formatted string that describes all of the things.
Things -> string"""
names=[o.name for o in things]
if len(names) == 0:
return 'nothing'
elif len(names) == 2:
names = ' and the '.join(names)
return 'the ' + names
else: #Here's where I need help!
names = ', the '.join(names)
return 'the ' + names
So at this point the function returns "the red rose, the pink rose, the white rose" which is great, but I need that last "and" to be put in between the pink rose and the white rose, and I can't use print. Any help? This is probably simple and I'm just missing it entirely OTL
Aucun commentaire:
Enregistrer un commentaire