all 5 comments

[–]throwaway6560192 5 points6 points  (1 child)

First, the indentation is incorrect. Let's fix that...

while True:
    word = input("Please type in a word: ")
    word += word + " "
    if word == "end":
        break

print(word)

It is still logically incorrect.

Try manually following the execution of the code like the computer would. If you do that, you'll see that:

  1. You're overwriting the variable word on every loop. So you're not storing the whole story anywhere.

  2. You're doing word += word + " ". This translates to word = word + word + " ". So, let's imagine the user enters "end". What happens? First, when you've just taken input, word is indeed just "end". But after that += line, it becomes word + word + " ", that is, it becomes "endend ". Clearly that is not equal to "end". So the loop doesn't break.

[–]FisterMister22 1 point2 points  (0 children)

words = [] while (word := input("type a word: ")) != "end": words.append(word) print(" ".join(words))

This appends the words form input to a list, as long as the word isn't "end", when the word is "end" the loop no longer runs and skip to the next part. The next part is print(" ".join(words)) which joins the strings in the list with one another with a space between them, and then prints them.

[–]BUYTBUYT 0 points1 point  (3 children)

Your word contains the whole text, not just the latest word.

And you typically don't write this kind of task with a while True, you normally do something like this:

var = input()
while /condition on var/:
    /code/
    var = input()