PYTHON round Forthis problem,we'll round an int value up to the next multiple of 10 ifits rightmost digit is 5 or more,so 15 rounds up to 20.Alternately,round down to the previous multiple of 10 if its rightmost digit is lessthan 5,so 12 rounds down to 10.Given 3 ints,a b c,return the sum oftheir rounded values.To avoid code repetition,write a separate helperdef round10(num): and call it 3 times.Example Output:roundsum(16,17,18) → 60roundsum(12,13,14) → 30roundsum(6,4,4) → 10 英语
网友回答
【答案】 $ pythonPython 2.7.3 (default, Jan 2 2013, 16:53:07) [GCC 4.7.2] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> def round_sum(*args):... return sum(map(lambda x: round(x, -1), args))... >>> round_sum(16, 17, 18)60.0>>> round_sum(12, 13, 14)30.0>>> round_sum(6, 4, 4)10.0>>> 追问: 能不能解析下。我刚刚接触对于语法不太熟悉。 追答: map(func, iterable) 对iterable里的每个元素调用func函数, 返回值组成新的列表 追问: 这个我当学习样本了。如果我用loop来解决这个问题的话你能不能给我个大概,在loop里我如何round作为数学白痴的我实在是太难了。 追答: # usage loopsummary = 0for item in iterable: summary += round(item, -1)