繁体   English   中英

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

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

我看过有关设置最大值的代码,但找不到有关设置最小值的任何内容,如果低于它,它应该会给出反馈,提示没有足够的单词。

你需要使用

。分裂()

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!")

所以 .split() 创建的列表的长度就是他们输入的单词数。 希望这可以帮助。

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

该脚本将一直循环,直到用户输入至少包含 5 个单词的句子。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM