Python123题库完美答案——将形如 5D, 30s 的字符串转为秒

  • 结果正确-100%
  • 代码规范-100%

题目链接(请点击)

import sys


def convert_to_seconds(time_str):
    time_str = time_str.lower()
    if time_str[-1] == "s":
        return float(time_str.replace('s', ''))
    elif time_str[-1] == "m":
        return float(time_str.replace('m', ''))*60
    elif time_str[-1] == "h":
        return float(time_str.replace('m', ''))*3600
    elif time_str[-1] == "d":
        return float(time_str.replace('d', ''))*3600*24


while True:
    line = sys.stdin.readline()
    line = line.strip()
    if line == '':
        break
    print(convert_to_seconds(line))

你可能感兴趣的:(Python123)