gpt4 book ai didi

python - 程序/循环仅部分工作

转载 作者:行者123 更新时间:2023-12-01 03:30:27 24 4
gpt4 key购买 nike

我正在编写一个刽子手脚本,但它无法正常工作。在这个例子中,我使用“curiosities”这个词作为猜测的词,因为它有三个“i”。F(或者由于某种原因样式在 block 中很奇怪):

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)

最佳答案

正如 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这是我前几天写的一对夫妇。

关于python - 程序/循环仅部分工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40996052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com