I'm a beginner at coding in Python and I've been practising with exercises CodeWars.
There's this exercise which basically wants you to recreate the display function of the "likes" on Facebook, i.e. how it shows the number likes you have on a post etc.
Here is my code:
def likes(names):
for name in names:
if len(names) == 0:
return 'no one likes this'
elif len(names) == 1:
return '%s likes this' % (name)
elif len(names) == 2:
return '%s and %s like this' % (names[0], names[1])
elif len(names) == 3:
return '%s, %s and %s like this' % (names[0], names[1], names[2])
elif len(names) >= 4:
return '%s, %s and %s others like this' % (names[0], names[1], len(names) - 2)
print likes([])
print likes(['Peter'])
print likes(['Alex', 'Jacob', 'Mark', 'Max'])
This prints out:
None
Peter likes this
Alex, Jacob and 2 others like this
My main issue here is that my first 'if' statement is not producing the string 'no one likes this' for when the argument: [] is empty. Is there a way around this problem?
Aucun commentaire:
Enregistrer un commentaire