简体   繁体   中英

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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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