我在做一个“风暴有多远”的程序用这个:雷声和闪电之间的秒数除以3。我还听说秒*340/1000更准确,但这不是问题的关键。你知道吗
from time import time
print("Press enter when you see flash and then again when you hear thunder:")
input() #first enter
begin = time()
input() #second enter
end = time()
length = end - begin
howfar = length * 340 #main calculation
print("Storm is " + "%.2f" % howfar + " meters from you.") #printing 2 numbers after decimal point
这应该很好,但有一个部分有问题:
input()
begin = time()
input()
end = time()
lenght = end - begin
我觉得这算不上时间,还是因为我时间不好,这算得很好? 你自己试试,告诉我效果是好是坏。你知道吗
我认为你应该看更多的代码和手册!Python手册非常优秀,而且易于使用。你知道吗
没什么意义。删除str(),因为
"%.2f" % howfar
已经生成了一个字符串。更优雅的是(尽管我认为你不应该对厘米的精度感兴趣):使用
raw_input()
代替input()
顺便说一下,lenght正确地写为length。你知道吗
相关问题 更多 >
编程相关推荐