Python编程练习

目录

 

1、编写一个函数,将形如 5D,30s, 的字符串转为秒‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬


1、编写一个函数,将形如 5D,30s, 的字符串转为秒‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬

=========   ======= ===================
Character   Meaning Example
=========   ======= ===================
s           Seconds '60s' -> 60 Seconds
m           Minutes '5m'  -> 5 Minutes
h           Hours   '24h' -> 24 Hours
d           Days    '7d'  -> 7 Days
=========   ======= ===================

demo:

input:1h

output:3600

import sys


def convert_to_seconds(time_str):
    if time_str[-1] in ['s', 'S']:
        return float(time_str[:-1])*1
    elif time_str[-1] in ['m', 'M']:
        return float(time_str[:-1])*60
    elif time_str[-1] in ['h', 'h']:
        return float(time_str[:-1])*3600
    elif time_str[-1] in ['d', 'D']:
        return float(time_str[:-1])*24*3600


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

 

你可能感兴趣的:(Python编程练习)