这是一个生成一些随机字符串直到生成给定字符串的程序。我无法理解修复部分中的索引的想法else。如果随机生成的字符与attemptThis[i]中的字符不匹配t[i],我们是否将字符存储在右侧attemptNext?之后当它再次检查时,只有一个字符存储在attemptThis?我不知道我问的方式是否正确。我在部分中得到了陈述的想法if。但是令人else:attemptNext += t[i]困惑。带有示例的 exp 将不胜感激。(来自 gfg 的代码)
import string
import random
possibleCharacters = string.ascii_lowercase + string.digits +
string.ascii_uppercase + ' ., !?;:'
t = "geek"
attemptThis = ''.join(random.choice(possibleCharacters)
for i in range(len(t)))
attemptNext = ''
completed = False
iteration = 0
while completed == False:
print(attemptThis)
attemptNext = ''
completed = True
for i in range(len(t)):
if attemptThis[i] != t[i]:
completed = False
attemptNext += random.choice(possibleCharacters)
else:
attemptNext += t[i]
iteration += 1
attemptThis = attemptNext
print("Target matched after " +
str(iteration) + " iterations")
三国纷争
幕布斯7119047
慕斯709654
相关分类