我正在处理一个刽子手脚本,但它没有正常工作。 在这个例子中,我使用了"好奇心"作为猜测的词,因为它有三个" i" s。 F(或某些原因造型很奇怪,所以在块中):
The desired output is "_ _ _ i _ _ i _ i _ _"
But I can only get to "_ _ _ i _ _ i _ _ _ _"
这里是代码的主要部分:
def alter(output_word, guessed_letter, the_word):
checkword = the_word
#print ("outputword:",output_word)
previous = 0
fully_checked = False
current_count = 0
while fully_checked == False:
global add_number
checkword = checkword[previous:]
add_number = previous + add_number
#print ("..",checkword)
if guessed_letter in checkword:
current_count = current_count + 1
print (">>>",current_count)
the_index = (checkword.index(guessed_letter))+add_number
# print (output_word)
# print (the_index)
# print (guessed_letter)
output_word= output_word[:the_index-1] + guessed_letter + output_word[the_index:]
previous = the_index+1
else:
fully_checked = True
checkword = the_word
add_number = 0
print (' '.join(output_word))
checklist(current_count)
def checklist(current_count):
print ("checklist")
print (current_count)
global total_count
total_count = total_count + current_count
print ("total count is", total_count)
代码中的其他地方触发了这一切(the_word是好奇心,guessed_letter是i):
if guessed_letter in the_word:
alter(output_word, guessed_letter, the_word)
答案 0 :(得分:1)
您将output_word
的索引与切片checkword
的索引混合在一起。
替换previous = the_index+1
带
previous = checkword.index(guessed_letter)+1
output_word= output_word[:the_index-1] + guessed_letter + output_word[the_index:]
应该是
output_word= output_word[:the_index] + guessed_letter + output_word[the_index+1:]
输出:
_ _ _ i _ _ i _ i _ _
答案 1 :(得分:1)
正如Derorrist指出的那样,你的代码逻辑存在一些缺陷,主要是你将output_word
的索引与checkword
的索引混为一谈。
我已修复了这些错误,并将您的代码压缩了一点。 add_number
不需要是全局的,通常你应该避免使用可修改的全局变量,除非你真的需要它们,因为它们会破坏代码的模块性。
total_count = 0
def alter(output_word, guessed_letter, checkword):
previous = add_number = current_count = 0
while True:
checkword = checkword[previous:]
if guessed_letter in checkword:
the_index = checkword.index(guessed_letter)
offset = the_index + add_number
print(">>>", current_count, the_index, offset)
output_word= output_word[:offset] + guessed_letter + output_word[offset + 1:]
previous = the_index + 1
add_number += previous
current_count += 1
else:
break
print(' '.join(output_word))
checklist(current_count)
return output_word
def checklist(current_count):
global total_count
print("checklist")
print(current_count)
total_count += current_count
print("total count is", total_count, end='\n\n')
# test
the_word = "curiosities"
output_word = "_" * len(the_word)
print(' '.join(output_word))
for guessed_letter in 'cisotrue':
output_word = alter(output_word, guessed_letter, the_word)
<强>输出强>
_ _ _ _ _ _ _ _ _ _ _
>>> 0 0 0
c _ _ _ _ _ _ _ _ _ _
checklist
1
total count is 1
>>> 0 3 3
>>> 1 2 6
>>> 2 1 8
c _ _ i _ _ i _ i _ _
checklist
3
total count is 4
>>> 0 5 5
>>> 1 4 10
c _ _ i _ s i _ i _ s
checklist
2
total count is 6
>>> 0 4 4
c _ _ i o s i _ i _ s
checklist
1
total count is 7
>>> 0 7 7
c _ _ i o s i t i _ s
checklist
1
total count is 8
>>> 0 2 2
c _ r i o s i t i _ s
checklist
1
total count is 9
>>> 0 1 1
c u r i o s i t i _ s
checklist
1
total count is 10
>>> 0 9 9
c u r i o s i t i e s
checklist
1
total count is 11
FWIW,有更简单的方法可以提高效率。例如,here是我前几天写的一对。