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

2024-04-18 20:22:36 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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


Tags: from程序youinputtime时间lengthend
1条回答
网友
1楼 · 发布于 2024-04-18 20:22:36

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

str("%.2f" % howfar)

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

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

使用raw_input()代替input()

顺便说一下,lenght正确地写为length。你知道吗

相关问题 更多 >