2017-11-11 18 views
-3

我完全不熟悉Python和編碼。我需要編寫一個允許用戶輸入許多行的代碼,當他們完成編寫多個句子時,他們輸入一段時間來停止程序,然後程序會告訴用戶輸入了多少個單詞。我會如何去做這件事?來自用戶的字數

這是我到目前爲止有:

print("Enter as many lines of text as you want.") 
print("When you're done, enter a single period on a line by itself.") 

while True: 
    print("> ", end="") 
    line = input() 
    if line == ".": 
     break 
    totalWords = line.split() 
    newWords = totalWords.append(line) 
wordCount = len(newWords) 
print("The number of words entered:" , wordCount, "") 
+2

「我想要X」類型的「問題」以及誰幫助我不喜歡我們在這裏,在這裏您必須展示您已經嘗試了什麼以及您遇到了哪些問題,因此我們將幫助您提供建議或可能的解決方案 – eyllanesc

+2

問題要求作業幫助必須包括迄今爲止解決問題所做的工作摘要,以及描述您解決問題的難度。 - https://stackoverflow.com/help/on-topic – skrx

+1

我建議您閱讀以下內容以改善您的問題:[我如何問一個好問題?](https://stackoverflow.com/help/how-to - 問) – eyllanesc

回答

0

你應該設置content退出循環。

content = [] 
while True: 
    line = input() 
    if line == ".": 
     break 
    words = line.split() 
    content.append(words) 

words_list = [item for sublist in content for item in sublist] 
print(len(words_list)) 

此外,該改變的序列/繪圖的項目大部分功能並返回None。因此newWords = totalWords.append(line)將始終返回None

+0

謝謝你一噸,這幫了我一噸。過去3-4個小時,我一直在強調這個問題。 –