我要打印代码: 火车明天13:36发车 . . 火车明天17:16开 火车每20分钟开一次
这是我迄今为止所尝试的
h = 13
m = 36
i = 20
while(True):
m = m + i
if m >= 60:
h = h + 1 and m = 0
if h > 17 and m > 16:
break
print("The train will leave at {0:0d}:{1:0d} tomorrow".format(h, m))
当运行代码时,我得到“SyntaxError:cannotassigntooperator” 我做错什么了
不要试图用这种天真的方式与时间共事。有那么多的用例和边缘案例需要考虑和处理
使用适当的时间对象(在本例中是} objects do no support ^{} calculations )
datetime
,因为是纯的^{输出
导致这个错误的问题是
h=h+1 and m=0
,如果您想在一行中执行此操作,您可以执行h, m = h + 1, m
,尽管我个人会将它拆分为两行除此之外,还有一些其他问题:
if h>17 and m>16:
上的缩进错误。只有在m
是第一个>= 60
,因为m
被重置为0
,而m
永远不会是>16
,所以while循环将永远运行通过以下方式解决此问题:
因为你重置了
时,可以通过重置m=0
,以后再也不会是16
16 -> 36 -> 56 -> 76 -> 0 -> 20 -> 40 -> 60 -> 0 -> ...
当m=m-60
超过60
m=m-60
来解决这个问题最终的脚本将类似于:
相关问题 更多 >
编程相关推荐