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