python 我如何修复此代码,以便在刽子手游戏的正确位置写入字母?

oknwwptz  于 2023-11-15  发布在  Python
关注(0)|答案(1)|浏览(68)

我正在上一门基于Python的计算机科学入门课。我正在做的一个项目是使用Python海龟图形制作一个刽子手游戏。我希望这段代码能够做的是写出用户猜对的单词的字母,并在用户猜错的时候画出刽子手的某个部分。现在,当用户猜对一个字母时,程序正在写这封信,但不是在正确的位置,有时根本不写。
在我目前的版本中,我有一个for循环,遍历当前单词的字母。如果用户猜测的字母与单词中的字母相同,乌龟(word_typer)的作用是将字母写在屏幕上正确的位置。我在while循环中有一个for循环,其中还有变量“place”来记录写的是哪封信,但这似乎还是不起作用。

wrong_guesses = 0
guess_letter = ""
wrong_letters = 0

play = input("The theme of this hangman is chemistry! Would you like to play? y/n ")

def run():
    global play, wrong_guesses, current_word, wrong_letters
    if (play.lower() == 'y'):
        new_word()
        place = 0
        while (wrong_guesses < 6):
            guess_letter = input('Please guess a letter ')
            letters_guessed.append(guess_letter)
            for letter in current_word:
                if guess_letter == letter:
                    word_typer.penup()
                    word_typer.goto(30 * place, 0)
                    word_typer.write(guess_letter, font=("Arial", 20, "bold"))
                    place += 1
            for i in current_word:
                if guess_letter != i:
                    wrong_letters += 1
            if wrong_letters == len(current_word):
                wrong_guesses += 1
                draw_hangman()
    else:
        print("Maybe next time!")

字符串

7kqas0il

7kqas0il1#

嗯,我没有找到足够的信息,从代码,你已经公布,你能把整个代码?
此外,我设法修改代码的方式,它将工作没有错误(它丢失了一些变量)。

wrong_guesses = 0
guess_letter = ""
wrong_letters = 0
letters_guessed = [] #stores the letter which are guessed by users

play = input("The theme of this hangman is chemistry! Would you like to play? y/n ")

def run():
    global wrong_guesses, current_word, wrong_letters
    if (play.lower() == 'y'):
        new_word() #should generate a new word and add it to current_word
        place = 0
        while (wrong_guesses < len(curent_word)):
            guess_letter = input('Please guess a letter ')
            letters_guessed.append(guess_letter)
            for letter in current_word:
                if letter == guess_letter:
                    word_typer.penup()
                    word_typer.goto(30 * place, 0)
                    word_typer.write(guess_letter, font=("Arial", 20, "bold"))
                    place += 1
                elif letter != guess_letter:
                    wrong_letters += 1

字符串
希望这段代码对你有帮助!

相关问题