Real or Made Up? چالش 6 از جلسهی 34، سخت
شاعر پنج جمله میسازد. برای هر جمله، اگر عیناً در POEMS بود کنارش بنویس (real)، وگرنه (made up) و made_up را یکی زیاد کن. یعنی بهجای print(text) بنویس print(text, "(real)") یا print(text, "(made up)"). خط آخر میگوید چند جملهی ساختگی ساخت که شبیه شعرهای واقعیاند. این توهم کوچک ماست!
POEMS = [
"the cat sat on the mat",
"the cat ran to the sun",
"the dog sat in the sun",
"the sun is warm and bright",
"the moon is round and white",
"a bird can sing in the tree",
]
table = {}
for line in POEMS:
words = line.split()
for i in range(len(words) - 1):
if words[i] not in table:
table[words[i]] = []
table[words[i]].append(words[i + 1])
import random
random.seed(8)
made_up = 0
for n in range(5):
line = ["the"]
while line[-1] in table and len(line) < 6:
line.append(random.choice(table[line[-1]]))
text = " ".join(line)
print(text)
print("made up:", made_up, "of 5")
راهنما و جواب این چالش · برگشت به درس جلسهی 34: کلمهی بعدی را حدس بزن
بیشتر بخوان: حلقه while در پایتون: آموزش با مثال و بازی حدس عدد