I'm completely new to Python and coding in general. I am needing to write a code that allows the user to input many lines and when they are finished writing their multiple sentences, they enter a single period to stop the program and the program will then tell the user how many words were inputted. How would I go about doing this?
Here is what I have so far:
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, "")
ACCEPTED]
You should set content out of loop.
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))
Besides, Most functions that change the items of sequence/mapping does return None. So newWords = totalWords.append(line) will always return None.