用Python写一个程序:给出一个英文句子,统计单词个数。
时间: 2024-05-08 13:15:51 浏览: 24
以下是Python程序:
```python
sentence = input("请输入英文句子:")
words = sentence.split()
count = len(words)
print("单词个数为:", count)
```
程序首先通过`input()`函数获取用户输入的英文句子,然后使用`split()`方法将句子分割成单词,存储在列表`words`中。接着,使用`len()`函数统计单词个数,并输出结果。
相关问题
编写python程序,给出一个英文句子,统计单词个数
好的,以下是 Python 代码:
```python
sentence = input("请输入一个英文句子:")
words = sentence.split()
print("单词个数为:", len(words))
```
你可以输入一个英文句子,程序会自动统计其中的单词个数并输出。
python编写程序,给出一个英文句子,统计单词个数
可以使用Python中的split()函数将句子分割成单独的单词,然后使用len()函数计算单词数目,示例代码如下:
sentence = "This is a sample sentence."
word_list = sentence.split()
num_words = len(word_list)
print("The number of words in the sentence is:", num_words)
输出结果为:The number of words in the sentence is: 5