likes([]) 返回--> "no one likes this" likes(["凌熙", "北北"]) 返回--> "凌熙 and 北北 like this" likes(["思寒", "蚊子", "花满楼", "西西"]) 返回--> "思寒, 蚊子 and 2 others like this"
对于4个或更多的名字,只会增加 and 2 others 中数字的数量。
def likes(name_list):
length=len(name_list)
if length ==0:
return 'no one likes this'
elif length==1:
return f'{name_list[0]} like this'
elif length==2:
return f'{name_list[0]} and {name_list[1]} like this'
elif length==3:
return f'{name_list[0]} and {name_list[1]} and {name_list[2]}like this'
else:
return f'{name_list[0]},{name_list[1]} and {length-2} others like this'
assert likes([]) == "no one likes this"
assert likes(["凌熙", "北北"]) == "凌熙 and 北北 like this"
assert likes(["思寒", "蚊子", "花满楼", "西西"]) == "思寒,蚊子 and 2 others like this"
def likes(name_list=None):
if not name_list:
return "no one likes this"
if len(name_list) < 4:
return " and ".join(name_list) + " like this"
if len(name_list) >= 4:
num = len(name_list) - 2
return ','.join(name_list[:2]) + f' and {num} others like this'
assert likes([]) == "no one likes this"
assert likes(["凌熙", "北北"]) == "凌熙 and 北北 like this"
assert likes(["思寒", "蚊子", "花满楼", "西西"]) == "思寒,蚊子 and 2 others like this"