[Python] How to end while loop when same value is inputted twice?

Discussão em 'Python' iniciado por Stack, Outubro 24, 2024.

  1. Stack

    Stack Membro Participativo

    How can I write a while loop that asks for a user for input, and breaks when the same value is used twice in a row?

    This is my current attempt, which tries to use len(set(word)) to get the previous word:

    story = ""

    while True:
    word = input("Give me a word: ")
    if word == "end" or word == len(set(word)):
    break
    story += word + " "

    print(story)

    Continue reading...

Compartilhe esta Página