试图为hangman修复此Python代码,但失败了

2024-10-02 06:22:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我在试着让这个刽子手密码生效。它应该从随机词数组中提取一个随机词,并将其分配给猜测词。这部分是有效的

然而,当一封信被输入时,不管是对是错,它都会被打印出来

 print("This letter is not in the word")
            print("You have", str(lives), "lives left")

这部分。此外,当我打印出guessword和randomletters的值时,guessword是一个字符串,而randomletters是一个数组。如何使其正确打印并正确运行代码? 完整代码如下

import random  # Random function

# Defines the game as a function
from typing import List


def hangman():
    lives = 6  # Lives counter
    entered_words = []  # Array for user input
    userletters = []  # Used for letters that have been inputted
    rndwords: List[str] = ["pumpkin", "red", "animal", "chocolate", "peas"]  # Words
    userletters: list

    # Code to ask the user if they want to add words to the array
    userinput = input("Add word: (y/n)")
    if userinput == "y":
        a = str(input("What word would you like to add?"))
        rndwords.append(a)
        print(a, "has been added")
    elif userinput == "n":
        print("Ok")
    else:
        print("Enter (y/n)")

    # Picks a word randomly from the array
    random_num = random.randint(0, (len(rndwords) - 1))
    guessword: list
    guessword = rndwords[random_num].lower()  # Picks a random word from the array
    print("The word is ", str(len(guessword)), "letters long")
    print("\n")
    c = "_ " * len(guessword)
    print(c)
    guessword: list
    # Checks if a correct character has been inputted
    while lives != 0:
        guess = input(print("Guess a letter: "))  # Asks user for input
        for i in range(0, 1):
            if guess in guessword:
                entered_words.append(i)
                print("This letter is in the word")
                userletters.append(i)
            else:
                pass

        if guessword[i] != guess:
            userletters.append(guess)
            lives = lives - 1
            print("This letter is not in the word")
            print("You have", str(lives), "lives left")

        if list(guessword) == userletters or list(guessword) == guess:
            print("You have won, good job")
            return
        # Ends the game when lives counter reaches 0
        if lives == 0:
            print("You have lost")
            print("The word is", guessword)
            print(guessword)
            print(userletters)
            return


hangman()

Tags: theininputifishaverandomlist
1条回答
网友
1楼 · 发布于 2024-10-02 06:22:03

这个代码有几个错误

首先,您在获取用户输入时出错

input(print("Guess a letter: "))

这个语句总是打印一个额外的None,您应该做的是

input("Guess a letter: ")

其次,您询问用户是否希望输入字符,但当他们回答no时,您没有做正确的事情,您继续询问字母。也许您应该在那里添加一个return语句

第三,如果您在程序中添加以下代码并进行调试,您可以看到您的算法实际工作

guess = input("Guess a letter: ")
print(f"guess {guess}")
print(guessword)

主要的问题是在下面的部分,这种说法是行不通的

if guessword[i] != guess:
    userletters.append(guess)
    lives = lives - 1
    print("This letter is not in the word")
    print("You have", str(lives), "lives left")

使用guessword[i] != guess语句,您显然总是在检查猜测词的第一个元素,这显然是错误的

您应该做的是删除for循环,该循环目前什么也不做,在while之前添加一个integer i,并在循环的and处增加它

相关问题 更多 >

    热门问题