擅长:python、mysql、java
<p>听起来好像你没料到会发生KeyError。如果是这样,那么使用<code>item.get()</code>可能只会隐藏错误。考虑使用<code>try</code>/<code>except</code>:</p>
<pre><code>try:
if item[param] != correct:
report_incorrect()
except KeyError:
print('Expected key {}'.format(param))
</code></pre>
<p>实际上,您应该返回代码并确定密钥不存在的原因。你知道吗</p>
<p>我试图看看你提供的链接,但代码还没有组织足够我容易阅读。它将有助于在顶部提供一些关于它应该做什么的评论。例如</p>
<pre><code>"""This code reads an input log file and a configuration file.
For each serial number in the configuration file, it outputs how
many times it occurs in the log file.
"""
</code></pre>
<p/><p>此外,它还将有助于将更多的东西组织到函数中,也许还可以组织到类中,并对每个函数的目标进行注释。也许当代码被清除后,问题就会消失。</p><p/>