如果单词中有字符则只执行一次操作

我创建了一个 python 刽子手游戏,除了一个与计算失败尝试次数相关的逻辑错误外,它运行良好。


    failed = 0

    for char in word:

        if char in letter_guess:

            print(char, end="")

            if letter_guess == word:

                print("")

                print('CONGRATULATIONS! YOU WON')

                sys.exit()

        else:

            failed = failed + 1

            print('_ ', end="")

            if failed == 15:

                print("GAME OVER! your word was", word)

                sys.exit() 

当玩家猜错一个字母时,它不是只添加一个,而是为每个不是玩家猜到的字母的字母添加一个。例如,如果单词是“star”,而玩家猜到了字母“e”,那么程序将添加 4 表示失败;每个字母一个。我不知道如何解决这个问题,以便它只执行一次这个特定的功能,因为我仍然需要它为每个错误的字母添加一个“_”。我想也许我可以将 4 个字母单词的尝试次数乘以 4,但我不确定这是否有效。


这是完整的代码


import random

import time

import sys


# GAME INTRODUCTION

print('HANGMAN')

name = input('Username: ')

print('Welcome', name, 'are you ready to play?')

answer = ''

print("type 'start' to begin")

while answer != 'start':

    time.sleep(0.7)

    answer = input()

print("OK! Let's begin")

print("pick the mode/difficulty of the game")

time.sleep(0.7)

print("1-Easy")

print("2-Medium")

print("3-Difficult")


# POSSIBLE WORDS IN GAME

words_4 = ['time', 'king', 'song', 'disk', 'meal',

           'cell', 'hair', 'menu', 'math']

words_5 = ['world', 'paper', 'hotel', 'queen', 'uncle',

           'night', 'hotel', 'shirt', 'pizza']

words_6 = ['person', 'tennis', 'camera', 'sector',

           'potato', 'safety', 'growth', 'thanks']



# CHOOSING GAME DIFFICULTY

mode = str(input())


if mode in ['Easy', '1', 'easy']:

    word_list = words_4

    letter_num = 4

    print("your word consists of 4 letters")

elif mode in ['Medium', '2', 'medium']:

    word_list = words_5

    letter_num = 5

    print("your word consists of 5 letters")

elif mode in ['Difficult', '3', 'difficult']:

    word_list = words_6

    letter_num = 6

    print("your word consists of 6 letters")

else:

    print("Mode does not exist!")


交互式爱情
浏览 122回答 3
3回答

慕侠2389804

您的代码中有几个问题。玩家永远不会获胜,因为letter_guess == word只有当该单词只有一个字母或用户每轮能够输入多个字母时才为真,我不认为这是这里的想法。在失败中,您不是在计算尝试次数,而是在计算丢失的字母。换句话说,您在每次尝试中都在计算“_”。我将在这里为您留下一些固定的代码,以便您可以将其用作参考:我创建了一个 python 刽子手游戏,除了一个与计算失败尝试次数相关的逻辑错误外,它运行良好。    failed = 0    for char in word:        if char in letter_guess:            print(char, end="")            if letter_guess == word:                print("")                print('CONGRATULATIONS! YOU WON')                sys.exit()        else:            failed = failed + 1            print('_ ', end="")            if failed == 15:                print("GAME OVER! your word was", word)                sys.exit() 当玩家猜错一个字母时,它不是只添加一个,而是为每个不是玩家猜到的字母的字母添加一个。例如,如果单词是“star”,而玩家猜到了字母“e”,那么程序将添加 4 表示失败;每个字母一个。我不知道如何解决这个问题,以便它只执行一次这个特定的功能,因为我仍然需要它为每个错误的字母添加一个“_”。我想也许我可以将 4 个字母单词的尝试次数乘以 4,但我不确定这是否有效。这是完整的代码import randomimport timeimport sys# GAME INTRODUCTIONprint('HANGMAN')name = input('Username: ')print('Welcome', name, 'are you ready to play?')answer = ''print("type 'start' to begin")while answer != 'start':    time.sleep(0.7)    answer = input()print("OK! Let's begin")print("pick the mode/difficulty of the game")time.sleep(0.7)print("1-Easy")print("2-Medium")print("3-Difficult")# POSSIBLE WORDS IN GAMEwords_4 = ['time', 'king', 'song', 'disk', 'meal',           'cell', 'hair', 'menu', 'math']words_5 = ['world', 'paper', 'hotel', 'queen', 'uncle',           'night', 'hotel', 'shirt', 'pizza']words_6 = ['person', 'tennis', 'camera', 'sector',           'potato', 'safety', 'growth', 'thanks']# CHOOSING GAME DIFFICULTYmode = str(input())if mode in ['Easy', '1', 'easy']:    word_list = words_4    letter_num = 4    print("your word consists of 4 letters")elif mode in ['Medium', '2', 'medium']:    word_list = words_5    letter_num = 5    print("your word consists of 5 letters")elif mode in ['Difficult', '3', 'difficult']:    word_list = words_6    letter_num = 6    print("your word consists of 6 letters")else:    print("Mode does not exist!")number = 0i = 0dash = ('_ ')word = random.choice(word_list)for char in word:    i = i + 1for number in range(i):    print(dash, end="")failed = 0guessed = ('')print("")#CHECKING CHARACTER PLAYER INPUTTEDtries = Truewhile tries is True:    print("")    print("")    letter_guess = input('Guess any letter: ')    for char in word:        if char in letter_guess:            print(char, end="")            if letter_guess == word:                print("")                print('CONGRATULATIONS! YOU WON')                sys.exit()        else:                        print('_ ', end="")            if failed == 15:                print("GAME OVER! your word was", word)                sys.exit()Pythonpython-3.x

蓝山帝景

我对您的代码做了一些小更改,而不是增加失败的循环,您必须将其放在循环之外for,但仍然在while循环内部。并且您需要将其放在循环sys.exit()外部while,因为您使用,如果字母猜测全部正确,则while Tries is True可以将其放在循环Tries = False内部,如果 , 则可以将 1 放在循环外部forforfailure = 15希望这是你想要达到的目标tries = Truewhile tries is True:    print("")    print("")    letter_guess = input('Guess any letter: ')    is_failed = False    for char in word:        if char in letter_guess:            print(char, end="")            if letter_guess == word:                print("")                print('CONGRATULATIONS! YOU WON')                tries = False                break        else:            is_failed = True            print('_ ', end="")    if is_failed:        failed = failed + 1        is_failed = False    if failed == 15:        print('')        print('')        print("GAME OVER! your word was", word)        tries = Falsesys.exit()如果您想存储以前的答案,并使您的代码更简洁:tries = Trueguessed_word = ['_' for _ in range(len(word))]while tries is True:    print("")    print("")    letter_guess = input('Guess any letter: ')    is_failed = False    for i, char in enumerate(word):        if char in letter_guess:            guessed_word[i] = char        else:            is_failed = True    print(' '.join(guessed_word))    if is_failed:        failed = failed + 1        is_failed = False    if failed == 15:        print('')        print("GAME OVER! your word was", word)        tries = False    if '_' not in guessed_word:        print("")        print('CONGRATULATIONS! YOU WON')        tries = Falsesys.exit()

婷婷同学_

我对您的代码做了一些小更改,而不是增加失败的循环,您必须将其放在循环之外for,但仍然在while循环内部。并且您需要将其放在循环sys.exit()外部while,因为您使用,如果字母猜测全部正确,则while Tries is True可以将其放在循环Tries = False内部,如果 , 则可以将 1 放在循环外部forforfailure = 15希望这是你想要达到的目标tries = Truewhile tries is True:    print("")    print("")    letter_guess = input('Guess any letter: ')    is_failed = False    for char in word:        if char in letter_guess:            print(char, end="")            if letter_guess == word:                print("")                print('CONGRATULATIONS! YOU WON')                tries = False                break        else:            is_failed = True            print('_ ', end="")    if is_failed:        failed = failed + 1        is_failed = False    if failed == 15:        print('')        print('')        print("GAME OVER! your word was", word)        tries = Falsesys.exit()如果您想存储以前的答案,并使您的代码更简洁:tries = Trueguessed_word = ['_' for _ in range(len(word))]while tries is True:    print("")    print("")    letter_guess = input('Guess any letter: ')    is_failed = False    for i, char in enumerate(word):        if char in letter_guess:            guessed_word[i] = char        else:            is_failed = True    print(' '.join(guessed_word))    if is_failed:        failed = failed + 1        is_failed = False    if failed == 15:        print('')        print("GAME OVER! your word was", word)        tries = False    if '_' not in guessed_word:        print("")        print('CONGRATULATIONS! YOU WON')        tries = Falsesys.exit()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python