我试图使用以下代码计算句子中的最大空格数:
def spaces(sentences):
textList = sentences.split('. ')
whiteList = [whitespaces.count(' ') for whitespaces in textList]
x = max(whiteList)
print(x)
但是,当它返回空白的数量时,这也会在第二行返回None
。这是怎么回事?
答案 0 :(得分:0)
您打印结果,但不归还。您需要在代码末尾添加return x
。
您可以查看this page,它解释了Python中print
和return
之间的区别。
答案 1 :(得分:0)
您必须从函数返回一个值,然后打印它的结果
def spaces(sentences):
textList = sentences.split('. ')
whiteList = [whitespaces.count(' ') for whitespaces in textList]
return max(whiteList)
print(spaces(sentences))