0
我正在为一个学校项目的hang子手脚本工作。我卡住了,因为一行不能正常工作,即使一个副本在不同的脚本中工作(也包括在下面)。这里的主要代码:复制代码不能运行相同(hangman)
def random_word(word_list):
global the_word
the_word = random.choice(word_list)
print (the_word)
print (".")
def setup():
global output_word
size = 0
for c in the_word:
size = size+1
output_word = size*"_ "
print (output_word, "("+str(size)+")")
def alter(output_word, guessed_letter, the_word):
checkword = the_word
print ("outputword:",output_word)
previous = 0
fully_checked = False
while fully_checked == False:
checkword = checkword[previous:]
print ("..",checkword)
if guessed_letter in checkword:
the_index = (checkword.index(guessed_letter))
print (output_word)
print (the_index)
print (guessed_letter)
# Line below just won't work
output_word= output_word.replace(output_word[the_index], guessed_letter)
print (output_word)
previous = the_index
fully_checked = True
def guessing():
global guessed_letter
guessed_letter = input("Enter a letter > ")
if guessed_letter in the_word:
alter(output_word, guessed_letter, the_word)
所以行
output_word= output_word.replace(output_word[the_index], guessed_letter)
应该打印出类似这样_ _ _ _ g^_ _ _(这个词摆动) 但它打印
_g_g_g_g_g_g_g
这是一个完整的输出:
costumed #the_word
.
_ _ _ _ _ _ _ _ (8) #output_word + size
Enter a letter > t
outputword: _ _ _ _ _ _ _ _
.. costumed #
_ _ _ _ _ _ _ _
3 # the_index
t #guessed_letter
_t_t_t_t_t_t_t_t #the new output_word
然而,在这不同的测试代码,一切正常:
output_word = "thisworkstoo"
the_index = output_word.find("w")
guessed_letter = "X"
output_word= output_word.replace(output_word[the_index], guessed_letter)
print (output_word)
输出: thisXorkstoo
这是否会取代所有实例字中的一个字母? – RnRoger
你需要它吗?我添加了一个shell示例来显示它的功能。 – rassar
也会看到更新后的答案 – rassar