抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

Q1: A Plus Abs B

Python’s operator module defines binary functions for Python’s intrinsic (固有的) arithmetic operators. For example, calling operator.add(2,3) is equivalent to calling the expression 2 + 3; both will return 5.

Note that when the operator module is imported into the namespace, like at the top of hw01.py, you can just call add(2,3) instead of operator.add(2,3).

Fill in the blanks in the following function for adding a to the absolute value of b, without calling abs. You may not modify any of the provided code other than the two blanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def a_plus_abs_b(a, b):
"""Return a+abs(b), but without calling abs.

>>> a_plus_abs_b(2, 3)
5
>>> a_plus_abs_b(2, -3)
5
>>> a_plus_abs_b(-1, 4)
3
>>> a_plus_abs_b(-1, -4)
3
"""
if b < 0:
f = _____
else:
f = _____
return f(a, b)

Use Ok to test your code:

1
python3 ok -q a_plus_abs_b✂️

Use Ok to run the local syntax checker (which checks that you didn’t modify any of the provided code other than the two blanks):

1
python3 ok -q a_plus_abs_b_syntax_check

评论