子手游戏坏了

时间:2019-02-15 17:39:30

标签: python python-3.x

我正在尝试制作这个子手游戏,但无法正常工作。有没有人建议修复它?

import random, os

print("^^^^^^^^^^THIS IS HANGMAN^^^^^^^^^^")
print("1. Play Game ")
print("2. Quit Game ")
choice = input("Please enter option 1 or 2")

if choice == "1":

    words = ["handkerchief", "accommodate", "indict", "impetuous"]
    word = random.choice(words)
    guess = ['_'] * len(word)
    guesses = 7

    while '_' in guess and guesses > 0:
        print(' '.join(guess))
        character = input('Enter character: ')

        if len(character) > 1:
            print('Only enter one character.')
            continue

        if character not in word:
            guesses -= 1

        if guesses == 0:
            print('You LOST!')
            break

        else:
            print('You have only', guesses, 'chances left to win.')

    else:
        print(''.join(guess))
        print('You WON, well done')

1 个答案:

答案 0 :(得分:0)

我认为,当您说游戏无法正常运行时-您是说正确猜测的角色没有出现?那是因为您没有更改guess变量。如果您不每次都更新,它将始终只包含_个字符:

for i, c in enumerate(word):
    if c == character:
        guess[i] = character

因此,如果将其添加到代码中(没有其他更改),则完整的游戏如下所示:

import random, os

print("^^^^^^^^^^THIS IS HANGMAN^^^^^^^^^^")
print("1. Play Game ")
print("2. Quit Game ")
choice = input("Please enter option 1 or 2")

if choice == "1":

    words = ["handkerchief", "accommodate", "indict", "impetuous"]
    word = random.choice(words)
    guess = ['_'] * len(word)
    guesses = 7

    while '_' in guess and guesses > 0:
        print(' '.join(guess))
        character = input('Enter character: ')

        if len(character) > 1:
            print('Only enter one character.')
            continue

        if character not in word:
            guesses -= 1

        for i, c in enumerate(word):
            if c == character:
                guess[i] = character

        if guesses == 0:
            print('You LOST!')
            break

        else:
            print('You have only', guesses, 'chances left to win.')

    else:
        print(''.join(guess))
        print('You WON, well done')

我对结构进行了一些更改,以使代码在我眼中更加直观:

import random

WORDS = ["handkerchief", "accommodate", "indict", "impetuous"]
MAX_GUESSES = 7

print("^^^^^^^^^^THIS IS HANGMAN^^^^^^^^^^")

while True:

    input('Press <ENTER> to start a new game or <CTRL>+<C> to quit.')

    word = random.choice(WORDS)
    guess = ['_'] * len(word)
    guesses = set()
    n = MAX_GUESSES

    while True:

        print('\nYour word:', ' '.join(guess))
        print('You have {} chances left.'.format(n))

        if '_' not in guess:
            print('Congratulations, you win!\n')
            break

        if n < 1:
            print('Sorry, no guesses left. You lose!\n')
            break

        character = input('Guess a new character: ')

        if len(character) != 1:
            print('You must enter exactly one character!')
            continue

        if character in guesses:
            print('You have already guessed that character!')
            continue

        guesses.add(character)

        if character not in word:
            n -= 1
            continue

        for i, c in enumerate(word):
            if c == character:
                guess[i] = character