1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Comparing a chosen word with a single-letter guess in Python

Discussão em 'Python' iniciado por Stack, Setembro 11, 2025.

  1. Stack

    Stack Membro Participativo

    I'm relatively new to programming, and I'm currently working on a Python Hangman game. I have a list of words, and my goal is to randomly select one word from the list. Then, I ask the user to guess a single letter. However, I'm facing a logical issue when comparing the chosen word (which is a complete word) with the user's single letter guess.

    To overcome this issue, I decided to split the chosen word into individual letters so that I can compare them with the user's guess. For example, I split "baboon" into "b", "a", "b", "o", "o", "n".

    Here's my code:

    Here is my code.

    import random

    word_list = ["aardvark", "baboon", "camel"]

    chosen_word = list(random.choice(word_list))

    guess = input("Input a letter: ").lower()

    for n in range(0, len(chosen_word)):
    if guess == chosen_word[n]:
    print("Correct")
    else:
    print("Wrong")


    Now, I noticed that the instructor's code achieves the same result with much simpler logic. They define a variable called letter, which appears to be a single letter string. However, in my opinion, comparing this letter with the complete word chosen_word doesn't make sense and should always result in a "Wrong" answer. For example, comparing "a" to "baboon" would not give a "Right" answer.

    Here is the instructor's code for reference.

    import random

    word_list = ["aardvark", "baboon", "camel"]

    chosen_word = random.choice(word_list)

    guess = input("Guess a letter: ").lower()

    if letter == guess in chosen_word:
    print("Right")
    else:
    print("Wrong")


    Am I misunderstanding something here? It seems that the instructor's code is simpler, but I don't see how the comparison of letter with the complete word chosen_word can produce correct results. I appreciate any insights or clarifications. Thank you!

    Continue reading...

Compartilhe esta Página