Hi! Need help with this exercise:
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.
Sample output
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.
Sample output
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
My code:
# Write your solution here
sentence = ""
previous_word = ""
while True:
word = str(input("Please type in a word: "))
if word == "end":
break
sentence += word + " "
previous_word = word
if previous_word == sentence:
break
print(sentence)
print(previous_word)
print(sentence)
I'm able to create a function for saving inputs to variable as well as a variable that only saves last input. I'm however not able to create a function for comparing last input to previous inputs and if matching break the loop.. How can I go about this?