如何计算用户在python中输入的文本字符串中有多少个单词

2024-10-01 17:27:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道是否有人能帮上忙,我对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)

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

任何帮助都将不胜感激!在


Tags: 工具of用户文本程序number列表if
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.

链接:

input()len()split()


示例:

^{pr2}$

好处:一行就可以完成!在

print('You wrote {} words!'.format(len(input("Enter some text, I will tell you how many words you wrote!: ").split())))
name = input ()
print len(name)

相关问题 更多 >

    热门问题