Python Coding Exercise #49: Story
Part of the series Python Practice Exercise — part 49
Before you start: Know input() · Know while True and break · Know string concatenation
The question
This exercise has two parts, and the second part changes the program from the first.
Part 1
Please write a program which keeps asking the user for words. If the user types in end, the program should print out the story the words formed, and finish.
Please type in a word: Once
Please type in a word: upon
Please type in a word: a
Please type in a word: time
Please type in a word: there
Please type in a word: was
Please type in a word: a
Please type in a word: girl
Please type in a word: end
Once upon a time there was a girl
Part 2
Change the program so that the loop ends also if the user types in the same word twice in a row.
Please type in a word: It
Please type in a word: was
Please type in a word: a
Please type in a word: dark
Please type in a word: and
Please type in a word: stormy
Please type in a word: night
Please type in a word: night
It was a dark and stormy night
What is new here: building a string in a loop
The program has to collect the words: one word in each loop, and print them all together at the end. For that, it needs a variable that grows as the loop goes on. Just like the counter in Exercise #47 grew by adding numbers, this one grows by adding text:
story = ""
story += "Once" + " "
story += "upon" + " "
story starts as an empty string, "". Each += joins a new piece of text to the end of it, and the result is stored back in story. After the two lines above, story is "Once upon ".
Part 1
For Part 1, the program only has to stop at end:
words = ""
while True:
word = input("Please type in a word: ")
if word == "end":
break
words += word + " "
print(words)
Each loop asks for a word. If it is end, the loop stops. Otherwise, the word and a space are added to words. After the loop, print(words) shows the finished story.
My Solution
My final program covers both parts:
words = ""
previous_word = ""
while True:
word = input("Please type in a word: ")
if word == "end" or word == previous_word:
break
words += word + " "
previous_word = word
print(words)
How My Solution Works
1. Two variables are created before the loop
words = ""is the story so far, and it starts empty.previous_word = ""is the word from the round before. It also starts empty, because before the first round, there is no previous word.
2. Each round asks for a word
word = input("Please type in a word: ") runs at the start of every round and replaces the old word with the new one.
3. Two reasons to stop, joined with or
if word == "end" or word == previous_word: is true when either thing happens:
- The user typed
end. - The user typed the same word as the one just before it.
If it is true, break ends the loop (see Exercise #43). Because the break comes before the += line, the word that ended the loop is not added to the story. That’s why the second night in the sample appears only once in the output, and end is never part of the story.
4. Otherwise, add the word and remember it
If the loop continues, two things happen:
words += word + " "adds the word and a space to the story.previous_word = wordstores the word, so that the next round can compare against it.
The order matters. previous_word = word comes last in the loop, after the check. If it came before the check, previous_word would already equal word, and the loop would stop on the very first word.
5. The story is printed after the loop
print(words) is not indented, so it runs once, when the loop is over.
Following the Part 2 sample
| Round | The user types | previous_word before | Stop? | words after |
|---|---|---|---|---|
| 1 | It | "" | No | It |
| 2 | was | It | No | It was |
| 3 | a | was | No | It was a |
| 4 | dark | a | No | It was a dark |
| 5 | and | dark | No | It was a dark and |
| 6 | stormy | and | No | It was a dark and stormy |
| 7 | night | stormy | No | It was a dark and stormy night |
| 8 | night | night | Yes | (loop ends, nothing added) |
After round 8, print(words) shows It was a dark and stormy night.
University Solution
story = ""
previous = ""
while True:
word = input("Please type in a word: ")
if word == "end" or word == previous:
break
story += word + " "
previous = word
print(story)
The University Solution is the same program as My Solution, with only the names changed (story instead of words, and previous instead of previous_word) and a blank line before the last print().
Things to notice
The story ends with a space. Each word is added together with a space after it, so the finished story is "Once upon a time there was a girl ", with a space at the end that can’t be seen on the screen.
Typing end first prints an empty line. The loop stops at once, and words is still the empty string. The same happens if the user presses Enter without typing anything at the very start, because an empty word equals the empty previous_word.
Only words right after each other count. go, stop, go doesn’t end the loop, because the repeated word isn’t next to the first one. Only go, go does.
Capital letters matter. night followed by Night isn’t a repeat, since == on strings is exact (as in Exercise #37). Both words are added to the story.
Key takeaways
- To build text in a loop, start with an empty string and add to it with
+=. - Add a space after each word, or the words will be joined together.
- To compare with the previous value, keep it in a variable and update it at the end of the round.
- Use
orto stop the loop for either of two reasons. - Put
breakbefore the line that adds the word, so the word that ends the loop isn’t added. - Print the finished result once, after the loop.
✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦
Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.