1

我想知道是否有人可以提供帮助;我对python很陌生。

我目前正在创建一个工具,它分析用户输入的文本并显示该短语属于哪个列表的反馈。

到目前为止,程序处于无限循环中,并计算总共输入了多少表达式,然后计算某个列表中某事发生了多少次。

if text in access:
    accessno +=1
    counter +=1
    print ('This could be classed as speech act 1: Access')
    print ("number of access hits ", accessno)
    print ("number of total hits ", counter)

所以我的问题是:如何让程序计算用户输入的句子中有多少单词?

任何帮助将非常感激!

4

2 回答 2

2

您可以通过以下简单方式进行操作。

s = input()
# input() is a function that gets input from the user
len(s.split())
# len() checks the length of a list, s.split() splits the users input into a word list.

链接:

输入() len() 拆分()


例子:

>>> s = input()
"hello world"
>>> s
'hello world'
>>> s.split()
['hello', 'world']
>>> len(s.split())
2

奖励:在一行中完成所有操作!

print('You wrote {} words!'.format(len(input("Enter some text, I will tell you how many words you wrote!: ").split())))
于 2013-04-07T13:24:18.243 回答
-1
name = input ()
print len(name)
于 2013-04-07T13:32:40.160 回答