答案 0 :(得分:4)
如果该字母当前为“^”且字母已正确猜到,则您希望将猜测的字母添加到结果中。否则,您想要添加之前视图中的内容
def guess(word, view, letter) :
result = ""
for i in range(0,len(word)) :
if view[i] == "^" and word[i] == letter:
result += word[i]
else :
result += view[i]
return result
使用Python的if-else
构造
true if condition else false
条件
def guess(word, view, letter) :
result = ""
for i in range(0,len(word)) :
result += word[i] if view[i] == "^" and word[i] == letter else view[i]
return result