擅长:python、mysql、java
<p>除了jamylak的建议之外,还有一些改进代码的通用指针:</p>
<ul>
<li>多行提示最好使用三重引号字符串语法而不是多个<code>print</code>语句编写。这样你只需要写一次<code>print</code>,而不需要所有额外的换行符(<code>\n</code>)</li>
</ul>
<p>示例:</p>
<pre><code>print """
Running on the stairs is dangerous!
You have a 70% chance to fall.
Run on the stairs anyway?
"""
</code></pre>
<ul>
<li><p>概率计算使用[1-100]范围内的随机整数,但使用浮点数可能更自然。(两种方法都可以。)</p></li>
<li><p>你不需要检查这个数字是否是<code><= 70</code>,然后再检查它是否是<code>>= 71</code>。根据定义(整数!)这些条件中只有一个是真的,所以实际上不需要同时检查这两个条件。</p></li>
</ul>
<p>示例:</p>
<pre><code>random_value = random.random() # random number in range [0.0,1.0)
if random_value < 0.7:
pass #something happens 70% of the time
else:
pass #something happens the other 30% of the time
</code></pre>
<p>或者更紧凑:</p>
<pre><code>if (random.random() < 0.7):
pass #something happens 70% of the time
else:
pass #something happens 30% of the time
</code></pre>