0

My code is for a simple hangman type game where the user is supposed to guess the a random word from a list letter by letter or guess the whole word. The comparison for when the user guesses the whole word doesn't execute. I hardcoded one of the possible random words into my code below as an example:

guess = "----"
letterCount = 8
letter = ""

x = 0
while letterCount > 0:
    temp1 = "word"
    letter = input("Guess a letter in the word or attempt to guess the whole word: ")
    if (len(letter) > 1):
        print ("this is the test word: word, this is letter:" + letter)
        if letter == "word":
            print ("You win!!!")
            letterCount = 0
    else:
        x = temp1.find(letter)
        while x != -1:
            x = temp1.find(letter)
            temp1 = temp1[:(x + 1)].replace(letter, '0') + temp1[(x + 1):]
            guess = guess[:(x + 1)].replace('-', letter) + guess[(x + 1):]
        print (("Your guess is now: " + guess))
    letterCount = letterCount - 1
    x = 0

If I guess the "word", it tells me that I guess word, it points out that I guess the word I should, but the if statement that should tell me I won never executes. Am I comparing these strings properly or is the problem something else?

1 Answer 1

1
>>> while letterCount > 0:
...     temp1 = "word"
...     letter = input("Guess a letter in the word or attempt to guess the whole word: ")
...     if (len(letter) > 1):
...         print ("this is the test word: word, this is letter:" + letter)
...         if letter == "word":
...             print ("You win!!!")
...             letterCount = 0
...     else:
...         x = temp1.find(letter)
...         while x != -1:
...             x = temp1.find(letter)
...             temp1 = temp1[:(x + 1)].replace(letter, '0') + temp1[(x + 1):]
...             guess = guess[:(x + 1)].replace('-', letter) + guess[(x + 1):]
...         print (("Your guess is now: " + guess))
...     letterCount = letterCount - 1
...     x = 0
...
Guess a letter in the word or attempt to guess the whole word: "word"
this is the test word: word, this is letter:word
You win!!!
>>>

You need to cast your input as a String for the String comparison test to work.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.