Cut the Handout چالش 2 از جلسهی 42، آسان
split_chunks(text) متن را با split("\n\n") (خط خالی) تکه کند، هر تکه را strip() کند و تکههای خالی را دور بریزد. خروجی test با 4 شروع میشود، بعد تکهی دوم (دربارهی درختهای درخشان) و آخر ['one', 'two'].
HANDOUT = """Nilu is a small made-up planet with two moons, Pip and Tov.
A day on Nilu lasts 30 hours, and a year lasts 200 days.
The glow trees of Nilu shine blue at night. They drink rain water
and grow 2 meters every year.
The sand foxes of Nilu sleep in the day and hunt at night.
They eat glow berries and small beetles.
Pip is the bigger moon. It turns around Nilu in 12 days.
Tov is tiny and turns around Nilu in 5 days."""
def split_chunks(text):
return [text]
# ---- test (do not change) ----
chunks = split_chunks(HANDOUT)
print(len(chunks))
print(chunks[1])
print(split_chunks("one\n\n\n\ntwo\n\n"))
راهنما و جواب این چالش · برگشت به درس جلسهی 42: امتحان کتابباز · چالش بعدی: Words and Scores
بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال