这个词的问题是:产生一个项目只需要2分7秒。不幸的是,143件产品生产完成后,制造商必须冷却5分13秒才能继续生产。编写一个程序,计算制造一定数量的产品所需的时间。你知道吗
测试编号为1340项。你知道吗
numItems = 1340
produceitem = 2 * 60 + 7 #2 minutes and 7 seconds
cooldown = 5 * 60 + 13 #5 minutes and 13 seconds
items_before_delay = 143
productiontime = 0
if numItems <= 143:
productiontime = produceitem * numItems
if numItems > 143:
productiontime = (produceitems * numItems) - (numItems / items_before_delay * cooldown)
print str(productiontime) + "seconds"
测试编号的输出应该是172997秒,但是我的程序输出它为167363秒。你知道吗
有人能告诉我我能做些什么来改进这个吗?你知道吗
你在减去冷却时间,而不是加上它。就这样。你知道吗
所以,改变这个:
…对此:
然而,当我们在这里时:
produceitem
,但使用了produceitems
。如果这真的奏效了,那可能是因为您在交互式解释器中很幸运,已经定义了produceitems
。你知道吗items_before_delay
,不要直接使用143,使用items_before_delay
。你知道吗if a <= b:
然后if a > b:
;只要把第二个改成else:
。你知道吗if
。如果numItems <= 143
,(numitems / items_before_delay * cooldown)
将是0,那么第二个版本仍然会给出正确的答案。你知道吗//
来截断整数除法,而不是/
。这意味着您的代码仍然可以在Python3.x中工作,或者如果有人执行__future__
语句,等等——更重要的是,这意味着人们可以阅读和理解您的代码,而不必猜测它是用于2.x还是3.xitems_before_delay
遵循PEP8的建议,但numItems
没有。你知道吗productiontime
这样的变量。你知道吗172997seconds
没有空格。你知道吗所以:
相关问题 更多 >
编程相关推荐