一个 Python 提示用户输入至少 5 个单词的句子的程序
A Python program that prompts the user to enter a sentence of at least 5 words
我看过有关设置最大值的代码,但找不到有关设置最小值的任何代码,如果低于它,它应该会给出提示提示的字数不足的反馈。
您需要使用
.split()
函数创建一个列表,其中包含您使用拆分的字符串中的每个单词的项目。
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 个单词的句子。
我看过有关设置最大值的代码,但找不到有关设置最小值的任何代码,如果低于它,它应该会给出提示提示的字数不足的反馈。
您需要使用
.split()
函数创建一个列表,其中包含您使用拆分的字符串中的每个单词的项目。
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 个单词的句子。