2 回答

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以創(chuàng)建字符串的“無空格”版本和“無空格,無下劃線”版本,然后比較每個(gè)字符以查看是否匹配非下劃線字符或是否已使用與下劃線對應(yīng)的字符。例如:
def match_with_gaps(match, guess):
nospace = match.replace(' ', '')
used = nospace.replace('_', '')
for a, b in zip(nospace, guess):
if (a != '_' and a != b) or (a == '_' and b in used):
return False
return True
print(match_with_gaps("te_ t", "tact"))
# False
print(match_with_gaps("a_ _ le", "apple"))
# True
print(match_with_gaps("_ pple", "apple"))
# True
print(match_with_gaps("a_ ple", "apple"))
# False

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊
這條線在這里:
if len(match) == len(myWord_noUnderLine)
不會(huì)給你你現(xiàn)在想要的。在“a_ple”示例中,空格和“_”都被刪除,因此您的 myWord_noUnderLine 變量將為“aple”,因此檢查“aple”和“apple”之間的長度匹配肯定會(huì)失敗
添加回答
舉報(bào)