作业帮 > 英语 > 作业

PYTHON round

来源:学生作业帮 编辑:搜搜做题作业网作业帮 分类:英语作业 时间:2024/06/06 21:50:27
PYTHON round
For
this problem,we'll round an int value up to the next multiple of 10 if
its 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 less
than 5,so 12 rounds down to 10.Given 3 ints,a b c,return the sum of
their rounded values.To avoid code repetition,write a separate helper
"def round10(num):" and call it 3 times.
Example Output:round_sum(16,17,18) → 60round_sum(12,13,14) → 30round_sum(6,4,4) → 10
PYTHON round
$ python
Python 2.7.3 (default, Jan  2 2013, 16:53:07) 
[GCC 4.7.2] on linux2
Type "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 loop
summary = 0
for item in iterable:
    summary += round(item, -1)