我写了这个函数。
# Function to count words in a string.
def word_count(string):
tokens = string.split()
n_tokens = len(tokens)
print (n_tokens)
# Test the code.
print(word_count("Hello World!"))
print(word_count("The quick brown fox jumped over the lazy dog."))
但结果是
2
None
9
None
而不是仅仅
2
9
除了布赖恩所说的,这段代码还演示了如何得到你想要的:
word_count
没有return
语句,因此它隐式返回None
。您的函数打印令牌的数量print (n_tokens)
,然后您的函数调用print(word_count("Hello World!"))
打印None
。相关问题 更多 >
编程相关推荐