Завершение моей секретной игры в слова и вывод числа пользователей в Python - PullRequest
0 голосов
/ 24 октября 2018

Я пытаюсь закодировать игру в стиле Hangman в Python.Нет предела тому, сколько раз пользователь может угадать.

У меня игра работает правильно, за исключением одной маленькой проблемы: когда пользователь угадывает слово целиком, игра не заканчивается и не сообщает ему, что он выиграл, и сколько угаданий ему потребовалось.

def main():

    print("Welcome to the Secret Word Game!")
    print()


    word = ["m","o","u","n","t","a","i","n"]

    guessed = []
    wrong = []

    guesses = 0

    while True:

        out = ""
        for letter in word:
            if letter in guessed:
                out = out + letter
            else:
                out = out + "*"

        if out == word:
            print("You guessed", word)
            break

        print("Guess a letter from the secret word:", out)

        guess = input()

        if guess in guessed or guess in wrong:
            print("Already guessed", guess)
            guesses +=1
        elif guess in word:
            print("Correct!",guess,"is in the secret word!")
            guessed.append(guess)
            guesses+=1
        else:
            print("Wrong!")
            wrong.append(guess)
            guesses+=1

    print()
    print(guesses)




main()

1 Ответ

0 голосов
/ 24 октября 2018

Вы не выходите, потому что ваше условие завершения не может быть истинным:

    if out == word:

out - строка длиной 8;word - это список из 8 односимвольных строк.Если вы измените инициализацию word на

    word = "mountain"

, тогда ваша программа будет работать нормально, получая отгаданные обновления и необходимое количество догадок.

Welcome to the Secret Word Game!

Guess a letter from the secret word: ********
n
Correct! n is in the secret word!
Guess a letter from the secret word: ***n***n
e
Wrong!
Guess a letter from the secret word: ***n***n
t
Correct! t is in the secret word!
Guess a letter from the secret word: ***nt**n
a
Correct! a is in the secret word!
Guess a letter from the secret word: ***nta*n
o
Correct! o is in the secret word!
Guess a letter from the secret word: *o*nta*n
i
Correct! i is in the secret word!
Guess a letter from the secret word: *o*ntain
f
Wrong!
Guess a letter from the secret word: *o*ntain
m
Correct! m is in the secret word!
Guess a letter from the secret word: mo*ntain
u
Correct! u is in the secret word!
You guessed mountain

9
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...