【发布时间】:2021-06-03 22:09:50
【问题描述】:
问题:
编写一个 Python 函数,clip(lo, x, hi),如果 x 小于 lo,则返回 lo;如果 x 大于 hi,则为 hi;否则为 x。对于这个问题,你可以假设 lo
不要对这个问题使用任何条件语句。相反,使用内置的 Python 函数 min 和 max。在开始这个问题之前,您可能希望阅读有关 min 的文档和有关 max 的文档,并在您的解释器中尝试一下这些函数。
这个函数接受三个数字并返回一个数字。
给定代码:
def clip(lo, x, hi):
'''
Takes in three numbers and returns a value based on the value of x.
Returns:
- lo, when x < lo
- hi, when x > hi
- x, otherwise
'''
我的代码已添加:
def clip(lo, x, hi):
'''
Takes in three numbers and returns a value based on the value of x.
Returns:
- lo, when x < lo
- hi, when x > hi
- x, otherwise
'''
if min(x, lo, hi) == x:
return lo
elif max(x, lo, hi) == x:
return hi
else:
return x
问题出在:我不能使用 ANY 条件。救命!
【问题讨论】:
-
将调用嵌套到最大值和最小值。
-
@AdamVandenberg 这是什么意思?
-
@user2066771:意思是互相调用。
标签: python