因此,基本上,我需要编写一个程序,该程序花费您跑公里多快的时间,然后将其转换为跑马拉松所需的时间。我完成了,但是问题是,如果有人输入的时间不正确(例如888或5:2555而不是8:30),应该可以处理
print "Marathon Time Calculator"
str_pace = raw_input("At what pace do you run a km? (e.g. 5:30): ")
if str_pace.isalpha():
print 'invalid time'
split_pace = str_pace.split(":")
minutes = int(split_pace[0])
seconds = int(split_pace[1])
str_pace = minutes * 60 + seconds
totalTime = str_pace * 42
hours = totalTime // 3600
timeLeft = totalTime % 3600
minutes = timeLeft // 60
seconds = timeLeft % 60
if len(split_pace[1]) >= 3:
print "Too many digits"
else:
print "You should complete a marathon in " + str(hours) + ":" + str(minutes) + ":" + str(seconds)
答案 0 :(得分:0)
我已经在python3中做到了
mutt