Challenge: Implementing a Threshold Checker with Closures
Tarefa
Swipe to start coding
Develop a closure that maintains a state and can be used to perform operations based on that state.
Let's create a closure that checks if a given value exceeds a specified minimum.
- Start by defining the outer function, which we'll name
threshold_checker
; - This function should accept a parameter named
threshold
; - Next, declare an inner function called
check
withinthreshold_checker
. This inner function should take one parameter,value
; - Inside the check function, return True if the value is greater than the threshold, and False otherwise. Use the
<
operator for this comparison; - Finally, ensure that the outer function,
threshold_checker
, returns the inner functioncheck
; - Assign the
threshold_checker
function to a variable namedgreater_than_10
, passing10
as the threshold value; - Run Code and Submit Task.
Solução
99
1
2
3
4
5
6
7
8
9
10
def threshold_checker(threshold):
def check(value):
return threshold < value
return check
# Usage
greater_than_10 = threshold_checker(10)
print(greater_than_10(12))
print(greater_than_10(8))
Tudo estava claro?
Obrigado pelo seu feedback!
Seção 4. Capítulo 7
single
99
1
2
3
4
5
6
7
8
9
10
def threshold_checker(threshold):
def check(value):
return ___ < ___
return ___
# Usage
greater_than_10 = ___(10)
print(greater_than_10(12))
print(greater_than_10(8))
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo