[英]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.