<p>当你得出的结果不正确或超出你的预期时,分解步骤以确定哪里出了问题通常是有帮助的。例如,以下是您的代码的分解方式略有不同,因此您可以更轻松地遵循逻辑流程</p>
<pre class="lang-py prettyprint-override"><code># ======================================
# Your code written slightly differently
# ======================================
def make_bricks(small, big, goal):
if small==goal:
return 1
elif big==goal:
return 2
elif goal%5==small:
return 3
elif small*1+big*5==goal:
return 4
elif goal%5<=small:
return 5
elif small+big!=goal:
return 6
elif goal-5!=small:
return 7
elif goal-small*1==big*5:
return 8
else:
return False
print(make_bricks(3, 1, 9))
print(make_bricks(3, 2, 9))
print(make_bricks(1, 4, 12))
</code></pre>
<p>这为您提供了一个提示,告诉您如何识别哪些评估可能没有按照您预期的方式工作。在解释之前,看看你是否能认出罪犯</p>
<blockquote class="spoiler">
<p> Evaluation #6 of your code returns <code>True</code> when <code>small + big does not equal the goal</code>. Because the result of this evaluation is <code>True</code> (it does not equal the goal), your script is returning <code>True</code>. In fact, your code will return <code>True</code> for every combination you test.<br/>
<br/>
In order to get to the results you expect, you'll need to correct the offending evaluation and refactor your code a bit to account for the <code>False</code> condition.</p>
</blockquote>
<p>一旦代码按您希望的方式工作,您就可以整合操作或重构,以使代码更加精简和高效</p>