How to detect a Hangman win after completion of word

Viewed 75

I appreciate my code is messy, but it works, for the most part, it just can't detect when I have won. I tried making Word_chooosed a string hoping it would help, but it didn't.

I have tried everything from my toolkit; what can I try next?

input:

import random
Words = ["Hades".upper(),"Zues".upper(), "pesidon".upper()]
Word_choosed = random.choice(Words)
Word_Choosed = list(Word_choosed)
a =[]
print(Word_Choosed)
for i in Word_choosed:
    a += "_"
print(" ".join(a))

wrong = 0
while wrong < 5:
    Incorrect = "yes"
    Guess = str(input("Guess a letter?\n").upper())
    for ltr in range(0, len(Word_choosed)):
        if Guess == Word_choosed[ltr]:

            a[ltr] = Guess
            print(a)
            wrong -= 1
            Incorrect = "no"

    if Guess != Word_choosed:
        wrong += 1
        if Incorrect == "yes":
                print(f"wrong you have {5 - wrong } chances left")
        elif wrong == 5:
                print("you lost")
    elif a == Word_Choosed:
        print("you won")
        break

Output:

['P', 'E', 'S', 'I', 'D', 'O', 'N']
_ _ _ _ _ _ _
Guess a letter?
p
['P', '_', '_', '_', '_', '_', '_']
Guess a letter?
e
['P', 'E', '_', '_', '_', '_', '_']
Guess a letter?
s
['P', 'E', 'S', '_', '_', '_', '_']
Guess a letter?
i
['P', 'E', 'S', 'I', '_', '_', '_']
Guess a letter?
d
['P', 'E', 'S', 'I', 'D', '_', '_']
Guess a letter?
o
['P', 'E', 'S', 'I', 'D', 'O', '_']
Guess a letter?
n
['P', 'E', 'S', 'I', 'D', 'O', 'N']
Guess a letter?
n
['P', 'E', 'S', 'I', 'D', 'O', 'N']
Guess a letter?
2 Answers

Cleaned it up a bit, and got it working nicely for ya!

import random
Words = ["Hades".upper(),"Zues".upper(), "pesidon".upper()]
Word_choosed = random.choice(Words)
Word_Choosed = list(Word_choosed)
a =[]
print(Word_Choosed)
for i in Word_choosed:
    a += "_"

wrong = 0
correct = 0
while correct < 5:
    print(''.join(a))
    Incorrect = "yes"
    Guess = str(input("Guess a letter?\n").upper())
    for ltr in range(0, len(Word_choosed)):
        if Guess == Word_choosed[ltr]:
            a[ltr] = Guess
            correct += 1
            Incorrect = "no"
    
    if Incorrect == "yes":
            wrong += 1
            print(f"wrong you have {5 - wrong } chances left")
    if 5 - wrong == 0:
            print("you lost")
            break
    elif a == Word_Choosed:
        print("you won")
        break

You added a elif: statement in the winning and lost code. So, python will only do one if and don't continue on your elif statement. You have to fix your code like this :

import random
Words = ["Hades".upper(),"Zues".upper(), "pesidon".upper()]
Word_choosed = random.choice(Words)
Word_Choosed = list(Word_choosed)
a =[]
print(Word_Choosed)
for i in Word_choosed:
    a += "_"
print(" ".join(a))
wrong = 0
while wrong < 5:
    Incorrect = "yes"
    Guess = str(input("Guess a letter?\n").upper())
    for ltr in range(len(Word_choosed)):
        if Guess == Word_choosed[ltr]:
            a[ltr] = Guess
            print(a)
            wrong -= 1
            Incorrect = "no"
    if Guess != Word_choosed:
        wrong += 1
        if Incorrect == "yes":
                print(f"wrong you have {5 - wrong } chances left")
        if wrong == 5: # I changed this to if
                print("you lost") 
                break # I added break you can remove it if you like
    if list(a) == list(Word_Choosed): # changed this to if
        print("you won")
        break
Related