简体   繁体   English

我怎样才能为刽子手拼出一个单词

[英]How can I spell a word out for Hangman

I'm having a lot of trouble figuring this out我很难弄清楚这一点

word = "hello"
hiddenWord = word
            
for i in range (0, len(hiddenWord)):
    hiddenWord = hiddenWord.replace(hiddenWord[i], "_")


userInput = input("Enter letter: ")

for i in range(0, len(word)):
    if word[i] == userInput:
        hiddenWord = hiddenWord.replace(hiddenWord[i], userInput)

print(hiddenWord)

I'm trying to have it find where in the variable "word" the user inputted letter is and replace the underscore in the same location in "hiddenWord" with that letter, so it will gradually spell out the word as the user guesses letters.我试图让它找到用户输入的字母在变量“word”中的位置,并将“hiddenWord”中相同位置的下划线替换为该字母,因此它会在用户猜测字母时逐渐拼出单词。

What it's doing instead is iterating through "word" and when it reaches a letter equal to one in "word", it will replace every character in "hiddenWord" with that letter.相反,它正在做的是遍历“word”,当它在“word”中到达一个等于 1 的字母时,它将用该字母替换“hiddenWord”中的每个字符。 So if I input l, it will iterate twice with no change, as it should, then change the string on the third and output "lllll" when the loop is finished.因此,如果我输入 l,它将迭代两次而不做任何更改,然后在循环完成时更改第三个字符串和 output "lllll"。 How would I get it to just change the relevant letters?我怎么能得到它来改变相关的字母?

If you add print statements to inspect what is happening in the line "hiddenWord = hiddenWord.replace(hiddenWord[i], userInput)", you will see that hiddenWord[i] is an underscore, and "userInput" is the letter you are looking for.如果您添加打印语句来检查“hiddenWord = hiddenWord.replace(hiddenWord[i], userInput)”行中发生的情况,您将看到 hiddenWord[i] 是一个下划线,而“userInput”是您所在的字母寻找。

Hence, if you were looking for the letter "e", the line will interpreted as "hiddenWord = hiddenWord.replace("_", "e") which replaces all 5 underscores to "e".因此,如果您正在寻找字母“e”,该行将被解释为“hiddenWord = hiddenWord.replace("_", "e"),它将所有 5 个下划线替换为“e”。

To change the relevant letters, modify the line of code in the "if-statement" to just change a letter at a specific index.要更改相关字母,请修改“if 语句”中的代码行以仅更改特定索引处的字母。 The string needs to be converted into a list first, as the "string" object does not support item assignment.需要先将字符串转换为列表,因为“字符串” object 不支持项目分配。 Once the for-loop is done, we join the characters in the list together, back into a string.一旦 for 循环完成,我们将列表中的字符连接在一起,重新组合成一个字符串。

See the code snippet below.请参阅下面的代码片段。

word = "hello"
hiddenWord = word
            
for i in range (0, len(hiddenWord)):
    hiddenWord = hiddenWord.replace(hiddenWord[i], "_")
    
userInput = input("Enter letter: ")

word_list = list(hiddenWord)

for i in range(0, len(word)):
    if word[i] == userInput:
        word_list[i] = userInput[0]

hiddenWord = ''.join(word_list)
print(hiddenWord)

Hope this helps.希望这可以帮助。

To make it more like the original game, you have some tweaks:为了使它更像原始游戏,您需要进行一些调整:

As strings are immutable, you have to convert hiddenWord to a list first (I did a multiply operator trick to create a list with same size of word , filled with _ chars).由于字符串是不可变的,因此您必须hiddenWord转换为列表(我做了一个乘法运算符技巧来创建一个具有相同大小的word列表,并用_字符填充)。

Another improvement is that range builtin function, when given only one parameter, assumes the count begins with 0 .另一个改进是内置 function 的range ,当仅给定一个参数时,假定计数以0开头。

Finally, I've added a loop that is done when the word is equal the guess .最后,我添加了一个循环,当word等于guess时完成。

word = "hello"
hiddenWord = list(len(word) * '_')
            
while True:    
    userInput = input("Enter letter: ")
    for i in range(len(word)):
        if word[i] == userInput:
            hiddenWord[i] = userInput
    guess = ''.join(hiddenWord)
    print(guess)
    if word == guess:
        print('Congratulations!')
        break

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hangman Game:给出一个单词,我怎么给它一个列表 - Hangman Game: One word is given, how can I give it a list 如何搜索字符串以查看我是否可以拼写一个单词 - How to search through a string to see if i can spell a word 如何使用索引方法查看一个单词是否与hangman中的用户输入匹配? - How can I use index method to see if one word matches user input in hangman? 如何创建一个打印随机选择的字母的代码,直到它可以拼出我想要的单词 - How do I create a code that prints randomly picked letters until it can spell the word I want 如何在同一 class AttributeError: 'Hangman' object has no attribute 'word_store' 的另一个 function 中使用属性 word_store - How can I use attribute word_store in another function of the same class AttributeError: 'Hangman' object has no attribute 'word_store' 在 Python 中:如何让我的代码根据我的输入打印出我可以拼写的所有可能的单词? - In Python: How can i get my code to print out all the possible words I can spell based on my input? 完成单词后如何检测刽子手获胜 - How to detect a Hangman win after completion of word 如何在 Hangman 游戏中进行字母检测? - How can I make letter detection in a Hangman game? 如何替换python中的重复单词(Hangman Game)! - How to replace the repeat word in python (Hangman Game)! Python Hangman 如何在单词中插入正确的字母? - Python Hangman how to insert the right letter in the word?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM