بتل کد Battle Codeدارد باز می‌شود…

A Random Poet چالش 4 از جلسه‌ی 34، متوسط

دمای ۱: عدد شانس (seed) با input می‌آید. شش بار: اگر کلمه‌ی آخر در table نبود break کن، وگرنه random.choice(table[word]) را به line اضافه کن. با عدد شانس 1 خروجی یک جمله است که از the شروع می‌شود. هر عدد شانس جمله‌ی دیگری می‌دهد، ولی هر عدد همیشه همان جمله را.

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(int(input()))

line = ["the"]
for i in range(6):
    word = line[-1]
print(" ".join(line))

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 34: کلمه‌ی بعدی را حدس بزن · چالش بعدی: Three Temperatures

بیشتر بخوان: حلقه for در پایتون: آموزش کامل با range و مثال