首页 > 解决方案 > 用猜出的字母代替破折号/ Hangman / Python

问题描述

我做了刽子手,正在使用这段代码。一切正常,我只想用相应位置正确猜测的字母替换列表 [lenghtDashes] 中的破折号。我在网上或这里也找不到任何信息。也许有人知道。我也愿意接受任何改进建议。

secWord = input("What is the word for others to guess?: ")
life = 10
#starting information

i = 1
while i < 10:
    print(".")
    i += 0.01
#dots so players can not see the word

print("you have 10 tries to guess the word")
print("you will have to choose between gessing a letter (l) or a word (w) in start of every turn")
print("however, if you guess the word incorrectly you will lose immediately")
ans = input("are you ready to begin?: ")
while ans != "yes":
    ans = input("now? ")
#rules

i = 1
while i < 10:
    print(".")
    i += 0.01
#dots for taking players to start

lenghtDashes = []
for letter in range(len(secWord)):
    lenghtDashes.append("_")
#making dashes list

print("let's begin")
print("the word has " + str(len(secWord)) + " letters.")
appended = "".join(lenghtDashes)
print(appended)
#info for players

usedLetters = []
guessedLetters = []
wordLis = []



while life > 0 and ans != secWord:
    print("are you guessing letter or a word?")
    guessThing = input("l / w : ")

    #for guessing a word
    if guessThing == "w":
        ans = input("What is the secret word?: ")

        if ans == secWord:
            print("Congratulations, you have guessed the right word\nYou won!")

        else:
            print("You guessed the word incorrectly\n The word was " + secWord + " \n You lost!")
            exit()
#=================================================================
    #for guessing a letter
    elif guessThing == "l":
        letList = [letter for letter in secWord]

        guLett = input("Guess the letter \n(single letter only): ")

        usedLetters.append(guLett)

        if guLett in letList:
            guessedLetters.append(guLett)
            letListPlus = [letter for letter, x in enumerate(letList) if x == guLett]
            letListPlusPlus = [num + 1 for num in letListPlus]

            print("///")
            print("///")
            print("///")

            print("Letters that you have used : " + str(usedLetters))
            print("Letter that are correct: " + str(guessedLetters))
            print("The position of letter " + str(guLett) + " is " + str(letListPlusPlus))

        else:

            print("///")
            print("///")
            print("///")

            print("incorrect guess, you have " + str(life - 1) + " guesses remaining")
            life = life - 1
            print("Letters that you have used are: " + str(usedLetters))
            print("Letter that are correct: " + str(guessedLetters))

#=================================================================

标签: pythonpython-3.xlist

解决方案


每当您需要显示正在进行的单词时(如"o_e_f_ow"),请调用此函数:

def wordWithDashes(guessedLetters, letList):
    return [letter if letter in guessedLetters else "_" for letter in letList]

它创建一个新列表,letList其中缺少的字母替换为"_"str如果你想要一个字符串结果,你可以使用它。

您无需跟踪lenghtDashes. 就像我提供的代码一样,只要您需要它,只需根据猜测的字母和实际单词来计算它。


推荐阅读