【发布时间】:2017-06-08 01:19:16
【问题描述】:
我刚刚开始自学 Python,我正在尝试编写一个代码来计算慢跑的结束时间。
到目前为止,我的代码如下所示:
def elapsed(t):
t = raw_input('Enter time (hh:mm:ss): ')
th, tm, ts = t.split(':')
return int(ts) + int(tm) * 60 + int(th) * 3600
def mile(m):
m = raw_input('How many miles? ')
return int(m)
start = elapsed('start')
warmup = elapsed('warmup')
wmile = mile('wmile')
tempo = elapsed('tempo')
tmile = mile('tmile')
cooloff = elapsed('cooloff')
cmile = mile('cmile')
hour = (start + warmup * wmile + tempo * tmile + cooloff * cmile) // 3600
minute = (start + warmup * wmile + tempo * tmile + cooloff * cmile - hour * 3600) // 60
second = (start + warmup * wmile + tempo * tmile + cooloff * cmile - hour * 3600) % 60
print('Your run ended at %02d:%02d:%02d' % (hour, minute, second))
在这段代码中,时间提示都是一样的:“输入时间(hh:mm:ss):”我希望每个提示都引用它的变量名,例如,“输入开始时间(hh:mm:ss) )”或“输入时间 (hh:mm:ss): (预热)”。有没有办法做到这一点?
注意:虽然这在技术上可能是重复的,但我已经检查了类似的问题,但认为所提供的问题和答案都比较不具体,因此决定还是问我的问题。
【问题讨论】:
-
你的意思是在提示中显示?
t = raw_input('Enter {} time (hh:mm:ss): '.format(t)) -
称为字符串插值。调用输入函数并不重要
标签: python python-2.7 function