救救孩子吧 python 作业

  • 1
新手上路,请多包涵

Fractions of hours
Write a program that converts a list of fractions of an hour into the number of milliseconds that they represent.
Your program must:
accept user input as a comma separated list of fractions, which can be expressed as either:
"x/y" - fraction
"xx.xx" - decimal
contain two functions:
parse(cs_fracstrs) - convert the comma separated string into a list of Fractions
frac_to_millis(frac) - convert a Fraction to the number of milliseconds of an hour it represents
Example

1/2, 0.2, 7/8 1800000, 720000, 3150000
Hint
There is a standard python module which will help you with your conversions. It is up to you to find it :).
回复
阅读 351
2 个回答

不建议孩子直接抄作业啊,以下只有骨干,仅供家长参考

def parse(cs_fracstrs):
    return cs_fracstrs.split(",")

def frac_to_millis(frac):
    return int(frac*60*60*1000)
    
while True:
    val=input()
    if not val:
        break
    l=parse(val)
    l=[eval(x) for x in l]
    l=[str(frac_to_millis(x)) for x in l]
    print(", ".join(l))
from fractions import Fraction

def parse(cs_fracstrs):
    return [Fraction(i) for i in cs_fracstrs.split(",")]

def frac_to_millis(frac):
    return int(frac*60*60*1000)
    
def convert(cs_fracstrs):
    return ",".join(str(frac_to_millis(frac)) for frac in parse(cs_fracstrs))
你知道吗?

宣传栏