<p>不要试图用这种天真的方式与时间共事。有那么多的用例和边缘案例需要考虑和处理</p>
<p>使用适当的时间对象(在本例中是<code>datetime</code>,因为是纯的<a href="https://bugs.python.org/issue1487389#msg54803" rel="nofollow noreferrer">^{<cd2>} objects do no support ^{<cd3>} calculations</a>)</p>
<pre><code>from datetime import datetime, timedelta
current_time = datetime.now().replace(hour=13, minute=16)
end_time = datetime.now().replace(hour=17, minute=16)
delta_minutes = 20
while current_time <= end_time:
print("The train will leave at {} tomorrow".format(current_time.strftime('%H:%M')))
current_time += timedelta(minutes=delta_minutes)
</code></pre>
<p>输出</p>
<pre><code>The train will leave at 13:16 tomorrow
The train will leave at 13:36 tomorrow
The train will leave at 13:56 tomorrow
The train will leave at 14:16 tomorrow
The train will leave at 14:36 tomorrow
The train will leave at 14:56 tomorrow
The train will leave at 15:16 tomorrow
The train will leave at 15:36 tomorrow
The train will leave at 15:56 tomorrow
The train will leave at 16:16 tomorrow
The train will leave at 16:36 tomorrow
The train will leave at 16:56 tomorrow
The train will leave at 17:16 tomorrow
</code></pre>