Python 刽子手:将*替换为玩家输入猜测

Python 刽子手:将*替换为玩家输入猜测,python,python-3.x,indexing,enumerate,Python,Python 3.x,Indexing,Enumerate,我一直在玩这个代码有一段时间了。我已经尝试了许多不同的方法,在随机生成的单词中找到玩家正确猜测的输入的索引——我认为我目前写的应该是有效的,但我担心我忽略了一个非常简单的错误。当我运行代码时,玩家的每一次猜测都被认为是正确的,而且当我尝试引用值player\u guess时,它不会出现在我的print语句print中(f“correct!{player\u guess}在单词中!” 我只是python的初学者(事实上,完全是编码),在stackoverflow之前的类似问题的帮助下,我花了大约8

我一直在玩这个代码有一段时间了。我已经尝试了许多不同的方法,在随机生成的单词中找到玩家正确猜测的输入的索引——我认为我目前写的应该是有效的,但我担心我忽略了一个非常简单的错误。当我运行代码时,玩家的每一次猜测都被认为是正确的,而且当我尝试引用值
player\u guess
时,它不会出现在我的print语句
print中(f“correct!{player\u guess}在单词中!”

我只是python的初学者(事实上,完全是编码),在stackoverflow之前的类似问题的帮助下,我花了大约8个小时试图自己解决这些问题,但最终我遇到了困难,因此非常感谢任何帮助

#random module to choose random word from word_list.txt
import random

#port in word_list.txt and create list
word_list = ['rarely', 'universe', 'notice', 'sugar', 'interference', 'constitution', 'we', 'minus', 'breath', 'clarify', 'take', 'recording', 'amendment', 'hut', 'tip', 'logical', 'cast', 'title', 'brief', 'none', 'relative', 'recently', 'detail', 'port', 'such', 'complex', 'bath', 'soul', 'holder', 'pleasant', 'buy', 'federal', 'lay', 'currently', 'saint', 'for', 'simple', 'deliberately', 'means', 'peace', 'prove', 'sexual', 'chief', 'department', 'bear', 'injection', 'off', 'son', 'reflect', 'fast', 'ago', 'education', 'prison', 'birthday', 'variation', 'exactly', 'expect', 'engine', 'difficulty', 'apply', 'hero', 'contemporary', 'that', 'surprised', 'fear', 'convert', 'daily', 'yours', 'pace', 'shot', 'income', 'democracy', 'albeit', 'genuinely', 'commit', 'caution', 'try', 'membership', 'elderly', 'enjoy', 'pet', 'detective', 'powerful', 'argue', 'escape', 'timetable', 'proceeding', 'sector', 'cattle', 'dissolve', 'suddenly', 'teach', 'spring', 'negotiation', 'solid', 'seek', 'enough', 'surface', 'small', 'search']

#Global variables
guesses = []
playing = True
lives = 7
#word generation
word = random.choice(word_list)
#create a display version on the generated word comprised of *
display = '*'* len(word)
#tracker of most recent player_guess
player_guess = ''

#create hangman graphics
def hangman():
    if lives == 7:
        print('____________')
        print('|/          ')
        print('|           ')
        print('|           ')
        print('|           ')
        print('|           ')
        print('|___________')
    if lives == 6:
        print('____________')
        print('|/        | ')
        print('|           ')
        print('|           ')
        print('|           ')
        print('|           ')
        print('|___________')
    if lives == 5:
        print('____________')
        print('|/        | ')
        print('|         O ')
        print('|           ')
        print('|           ')
        print('|           ')
        print('|___________')
    if lives == 4:
        print('____________')
        print('|/        | ')
        print('|         O ')
        print('|         | ')
        print('|           ')
        print('|           ')
        print('|___________')       
    if lives == 3:
        print('____________')
        print('|/        | ')
        print('|        _O ')
        print('|         | ')
        print('|           ')
        print('|           ')
        print('|___________')
    if lives == 2:
        print('____________')
        print('|/        | ')
        print('|        _O_')
        print('|         | ')
        print('|           ')
        print('|           ')
        print('|___________')
    if lives == 1:
        print('____________')
        print('|/        | ')
        print('|        _O_')
        print('|         | ')
        print('|        /  ')
        print('|           ')
        print('|___________')
    if lives == 0:
        print('____________')
        print('|/        | ')
        print('|        _O_')
        print('|         | ')
        print("|        / \ ")
        print('|           ')
        print('|___________')
        print("You lose")

def guess_input():
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    try:
        player_guess = str(input("\nSelect a letter between A-Z: ")).lower()
    except:
        print("\nThat was not a letter between A-Z, try again...")
    else:
        if len(player_guess) > 1:
            print("\nPlease only guess 1 letter - no cheating!")
        elif player_guess in guesses:
            print("\nYou have already guessed this letter, try again...")
        elif player_guess not in alphabet:
            print("\nThat was not a letter between A-Z, try again...")
        else:
            guesses.append(player_guess)
            return player_guess

def guess_checker():
    global lives, display, word, player_guess
    if player_guess in word:
        print(f"Correct! {player_guess} is in the word!")
        for i, letter in enumerate(word):
            if letter == player_guess:
                display[i] = player_guess
    else:
        lives -= 1

def win_check(word):
    if '*' not in display:
        print("Congratulations, you win!")
    else:
        False

############################# MAIN PROGRAM ####################################

#Introduction
print('Welcome to HANGMAN, I have randomly generated a word for your game. You have 7 lives - good luck!')

while playing == True:
    if lives > 0:
        #Guess input
        hangman()
        print('\n')
        print(display)
        guess_input()
        guess_checker()
        win_check(display)
        if win_check == True:
            playing = False
        if win_check == False:
            continue
    elif lives == 0:
        hangman()
        playing = False

函数
guess\u input
中的变量
player\u guess
不是具有该名称的全局变量。如果你能加上

global player_guess
。。。在这个功能中

但是,最好避免(或至少限制)使用
global
。相反,在主程序中捕获
guess\u input
返回的值,然后将其作为参数传递给
guess\u checker
,如下所示:

guess_checker(guess_input())
这样,您就不再需要该全局变量——删除相应的
public
指令,并为
guess\u checker
定义参数:

def guess_checker(player_guess):
评论 虽然不是您的问题,但代码中还有几个其他问题。一个主要问题是
display
是一个字符串,因此您不能执行
display[i]=player\u guess
——它将给出一个例外


请检查我在哪里改进和纠正了几件事。检查注释。

无需将
input()
的结果强制转换为字符串。如果my_bool或my_bool不是,则可以编写只检查布尔值的if语句。这也适用于
while
循环。正如Trincot在他们的回答中所说的那样,避免使用全局变量,而使用获取和返回值的函数。要获取小写ASCII字母列表,请使用
string.ASCII\u lowercase
。那些检查剩余生命数的
if
语句可以替换为
elif
语句。实际上,我认为你可以去掉很多主循环。一个
while True:
你打破了它,或者更好的是,一个
while lifes>0:
就可以了。感谢您的快速响应,我稍后会做出这些调整并回复您。请参阅我的答案的补充,因为我想您仍然会发现问题……非常感谢您的帮助。我检查了您的附加信息,它工作正常。在清除全局变量和尽可能使用参数方面有很好的经验。为什么不删除看似无用的try/except,并用
elif
s替换许多
if
s呢?这是一件小事,但是
字母表
字符串可以被
字符串.ascii\u小写字母
替换@AndyMac@AlexanderC埃西勒,我想问题不在于这个。我提供的链接不是答案的关键,也不能被认为是完整的代码审查。否则我将不得不在答案中嵌入该代码。请随意发布您自己的答案。