简体   繁体   English

Python 提示用户输入至少5个单词的句子的程序

[英]A Python program that prompts the user to enter a sentence of at least 5 words

I've seen code on setting a max but can't find anything on setting a minimum and if its below it should give feedback that there is not enough words prompted.我看过有关设置最大值的代码,但找不到有关设置最小值的任何内容,如果低于它,它应该会给出反馈,提示没有足够的单词。

You need to use the你需要使用

.split() 。分裂()

function that creates a list with an item for every word in the string that you use split on. function 创建一个列表,其中为您使用拆分的字符串中的每个单词创建一个项目。

sentence = input("Type a sentence of at least 5 words")
words = sentence.split()
if len(words) < 6:
    print("You need to type more words!")

So the length of the list that.split() created is how many words they typed.所以 .split() 创建的列表的长度就是他们输入的单词数。 Hope this helps.希望这可以帮助。

if len(sentence.split(" ")) < 6:
    [Do something]
while True:
    sent=input('Enter a sentence of at least five words>>')
    if len(sent.split(' '))>=5:
        break

This script will keep loop till the user enter a sentence with at least 5 words.该脚本将一直循环,直到用户输入至少包含 5 个单词的句子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python程序提示用户输入数字直到他们输入0,然后程序添加偶数和奇数 - Python program prompts user to enter number until they enter 0, then program adds even and odd integers 使用 python 编程语言在句子中大写或连接单词的程序 - program to capitalize or join words in a sentence using python programming language 修改此程序,使其在创建 window 之前提示用户输入所需的背景颜色 - Modify this program so that before it creates the window, it prompts the user to enter the desired background color Python清理句子中的单词 - Python cleaning words in a sentence 将句子标记为单词 python - tokenize sentence into words python 是否有提示用户输入特定数字的代码? - Is there a code where it prompts the user to enter specific numbers? 要求用户输入文件以使用 python 运行程序 - ask user enter a file to run the program with python 提示用户输入单词并在python中打印包含单词所有字符的文件中的所有单词 - Prompts the user for a word and prints all words in the file containing all characters of the word in python Python计算拆分句子的单词? - Python count words of split sentence? 突出显示python中句子中的特定单词 - Highlight specific words in a sentence in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM