Challenge: Text Cache
Tarefa
Swipe to start coding
Implement the string_cache()
function that returns the inner()
function that takes the string value and adds it to the enclosed text.
- Create the
text
variable with the value""
(empty string) in thestring_cache()
function. - The
inner()
function should take thestring
argument with the default value""
. - Make the
text
non-local variable changeable inside theinner()
function. - If the
string
argument is not an empty string, add this string to thetext
non-local variable. After the first string addition, the addition should be with space. - The
inner()
function should return thetext
enclosed variable. - The
string_cache()
should return theinner()
function without calling. - Call the
string_cache()
function and assign the result to thetext_cache
variable.
Solução
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def string_cache():
text = ""
def inner(string=""):
nonlocal text
if string != "" and isinstance(string, str):
if text != "":
text += " " + string
else:
text += string
return text
return inner
text_cache = string_cache()
print(text_cache())
print(text_cache("It's"))
print(text_cache("a"))
print(text_cache("text"))
print(text_cache("cache."))
print(text_cache())
Tudo estava claro?
Obrigado pelo seu feedback!
Seção 2. Capítulo 4
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def string_cache():
___ = ___
def inner(___=___):
___ text
if string != "" and isinstance(string, str):
if text != "":
text += " " + ___
else:
___ += string
return ___
return ___
text_cache = ___()
print(text_cache())
print(text_cache("It's"))
print(text_cache("a"))
print(text_cache("text"))
print(text_cache("cache."))
print(text_cache())
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo