![](/img/trans.png)
[英]Python program prompts user to enter number until they enter 0, then program adds even and odd integers
[英]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.