You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python编写的Wordle破解器故障排查与修复咨询

Wordle辅助程序故障修复:无匹配字母时无输出问题

问题根源

  1. 大小写不统一:爬取词表时未统一转换为小写,可能导致输入的小写单词与词表中的单词无法匹配。
  2. 匹配逻辑缺陷:当反馈为全下划线(无匹配字母)时,matches计算始终为0,而best_match初始值为0,导致matches > best_match条件永远不触发,best_fit保持为空,无输出。
  3. 评分规则缺失:未针对无匹配字母的场景设计合理的筛选规则,无法选出最优候选词。

修复步骤

1. 修复词表爬取代码(统一大小写)

将爬取到的单词全部转为小写,避免大小写不一致导致的匹配错误:

import requests
from bs4 import BeautifulSoup

webpage = 'https://www.wordunscrambler.net/word-list/wordle-word-list'
webpage_response = requests.get(webpage)
content = webpage_response.content
soup = BeautifulSoup(content, 'html.parser')

words = {}
for a in soup.find(class_='content').find_all('a'):
    text = a.get_text().lower()  # 统一转为小写格式
    if len(text) == 5:
        words[text] = [letter for letter in text]

with open('wordle_words.py', 'w') as file:
    file.write('words = ' + str(words))

2. 修复辅助程序逻辑(优化匹配与评分)

调整评分规则,针对无匹配字母的场景采用独特字母数量作为筛选依据,同时统一输入大小写、优化分数计算逻辑:

from wordle_words import words

starting_word = 'audio'

print('Start by entering the word "audio".')

wrong_letters = set()
matching_letters = set()

for i in range(5):
    attempted_word = input('What word did you try? ').lower()  # 统一输入为小写
    feedback = input('What were the matching letters? Uppercase for green letters, lowercase for yellow letters and underline for gray ones. "A_Do_": ')

    # 更新错误字母:提取反馈中的有效字母,排除下划线
    feedback_chars = [c.lower() for c in feedback if c != '_']
    wrong_letters.update([c for c in attempted_word if c not in feedback_chars])
    # 更新匹配字母:收集反馈中的非下划线字母
    matching_letters.update([c.lower() for c in feedback if c != '_'])

    # 筛选候选词:包含所有匹配字母,不包含错误字母
    matching_words = {word: letters for word, letters in words.items()
                      if matching_letters.issubset(set(letters)) and wrong_letters.isdisjoint(set(letters))}

    best_fit = ''
    best_score = -1  # 初始值设为-1,确保第一个候选词能被选中

    for word, letters in matching_words.items():
        score = 0
        # 绿色字母评分:位置正确加2分(权重更高)
        for index in range(5):
            if feedback[index].isupper():
                if letters[index] == feedback[index].lower():
                    score += 2
            # 黄色字母评分:字母存在但位置错误加1分
            elif feedback[index].islower():
                if feedback[index] in letters and letters[index] != feedback[index]:
                    score += 1
        # 无匹配字母场景:用单词的独特字母数量作为评分,优先选覆盖更多新字母的词
        if not matching_letters:
            score = len(set(letters))
        
        if score > best_score:
            best_fit = word
            best_score = score

    print(best_fit + '\n' if best_fit else 'No matching words found.\n')

修复效果

当输入起始词audio并反馈_____时,程序会从候选词中选出独特字母最多的单词(如jewel),正常输出结果。

内容的提问来源于stack exchange,提问作者Gabriel Marquetto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 02:05:58