def likes(names):
#your code here
length = len(names)
if not names:
return 'no one likes this'
if length == 1:
return '{} likes this'.format(*names)
if length == 2:
return '{} and {} like this'.format(*names)
if length == 3:
return '{}, {} and {} like this'.format(*names)
if length >= 4:
return '{}, {} and {} others like this'.format(names[0], names[1], length-2)
论坛中大神的回复:
def likes(names):
n = len(names)
return {
0: 'no one likes this',
1: '{} likes this',
2: '{} and {} like this',
3: '{}, {} and {} like this',
4: '{}, {} and {} others like this',
}[max(4, n)].format(*names[:3], n-2)