题目:
我的代码:
time = ["下午", 5, 21, 34]
if "下午" in time:
time[1] += 12
time[1] = time[1]*3600
time[2] = time[2]*60
x = time[1]+time[2]
result = x + time[3]
result1 = result / 86400
print(f"下午5点21分34秒换算成秒为{result}秒,占一整天秒数的比例为{result1}")
标准答案:
time = ["下午", 5, 21, 34]
if time[0] == "下午":
time[1] += 12
def time2sec(x):
sec = x[0]*3600 + x[1]*60 +x[2]
allsec = 24*3600
re = sec / allsec
return (sec, re)
a = time2sec(time[1:])
print(f"下午5点21分34秒换算成秒为{a[0]}秒,占一整天秒数的比例为{a[1]}")
简单理解一下第10行代码 a = time2sec(time[1:]) :
- 差不多新创了一个列表
time[1 : ] = [ 5, 21, 34]
a = time2sec( [ 5, 21, 34] ) = ( sec, re )
= ( a[0], a[1] )
因为不怎么会用函数,所以就用了别的方法编写,还要多加练习啊。