python,程序计时:风暴有多远?

zqry0prt  于 2021-06-21  发布在  Storm
关注(0)|答案(1)|浏览(219)

我在做一个“风暴有多远”的程序用这个:雷声和 lightning 之间的秒数除以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

我觉得这算不上时间,还是因为我时间不好,这算得很好?你自己试试,告诉我效果是好是坏。

anhgbhbe

anhgbhbe1#

我认为你应该看更多的代码和手册!python手册非常优秀,而且易于使用。

str("%.2f" % howfar)

没什么意义。删除str(),如下所示 "%.2f" % howfar 已生成字符串。更优雅的是(尽管我认为你不应该对厘米的精度感兴趣):

print("Storm is %.2f meters from you." % howfar)

而不是 input() 使用 raw_input() 顺便说一下,lenght正确地写为length。

相关问题