[英]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.