[Python] hangman python printing issue as beginner

Discussão em 'Python' iniciado por Stack, Janeiro 30, 2025.

  1. Stack

    Stack Membro Participativo

    The problematic section has been separated and rewritten from the main code. It should be printing the correctly guessed letters along with the ones not yet guessed as _. However, the result is that the correct guesses will be printed out with excessive underscores.

    As following:

    camel
    _____
    Guess a letter: c
    c____
    Guess a letter: a
    c_____a___


    The problematic code:

    import random as r
    word_list = ["aardvark", "baboon", "camel"]
    chosen_word = r.choice(word_list)
    print(chosen_word)
    placeholder = ""
    length = len(chosen_word)
    for position in range(length):
    placeholder += "_"
    print(placeholder)
    display = ""
    while 1 > 0:
    guess = input("Guess a letter: ").lower()
    for letter in chosen_word:
    if letter == guess:
    display += letter
    else:
    display += "_"
    print(display)

    Continue reading...

Compartilhe esta Página