samedi 18 avril 2020

I am trying to write a code for something similar to hangman, but it is not working

I'm trying to code a hangman kind of thing but even after the word is guessed correctly the loop continues. It seems like the bit that isn't working is the if word == ("".join(userword)) bit at the end, but I don't know what I'm doing wrong .

import random
word = random.choice(["apple", "orange", "pineapple"])
word = list(word)
life = 5
userword = []

for i in range (len(word)):
    userword.append("_")

while life > 0: 
    print("".join(userword))
    print("enter a letter")
    letter = input()
    letterinword = False
    for i in range(len(word)):
        if word[i] == letter:
            userword[i] = letter
            letterinword = True

    if letterinword == False:
        life = life - 1

    if word == ("".join(userword)):
        break

Aucun commentaire:

Enregistrer un commentaire