求求!
一辆汽车从苏州出发前往上海,每小时行驶80公里,两地距离为100公里。假设出发时间为当前时间,编写一个程序,计算并输出到达上海的时间,格式hh:mm:ss(时:分:秒)。最简单基础做法就好,新手呜呜 import time
print(time.strftime('%H:%M:%S', time.localtime(time.time() + 4500))) from datetime import datetime, timedelta
v = 80
s = 100
delta = timedelta(hours=s/v)
start = datetime.now()
print((start+delta).strftime("%H:%M:%S")) 如果不想用导入模块,可以自己写:class Time:
def __init__(self, hh, mm, ss):
self.hh = hh
self.mm = mm
self.ss = ss
def add(self, speed, distance):
ratio = distance / speed
second = 3600 * ratio
self.ss += second
hour = minute = 0
if self.ss >= 60:
minute, second = divmod(self.ss, 60)
self.ss = second
self.mm += minute
if self.mm >= 60:
hour, minute = divmod(self.mm, 60)
self.mm = minute
self.hh += hour
def __str__(self):
return "%02d:%02d:%02d"%(self.hh, self.mm, self.ss)
time = Time(11, 30, 00) # 假设现在 11点 30分
time.add(80, 100)
print(time) import time
t = time . localtime(100 / 80 * 3600 + time . time())
print(time . strftime('%Y-%m-%d %H:%M:%S' , t))
页:
[1]