Skip to content

نشاط

Poetry generator

متوسط | MakeCode, Python | شاشة LED, مقياس التسارع | التدقيق النحوي, الشعر, عشوائي

الخطوة 1: اصنعها

ماذا يعني؟

The poetry generator project creates random phrases that you can use in a poem, as a password or to start a story. It's nice to share especially poetic or funny phrases with your friends.  

هذان الفيديوهان يعرضان لك ما ستصنعه وكيفية برمجته:

What will you learn?

You will learn about arrays, a special kind of variable. Arrays are a useful way of storing data in lists.

كيف يعمل ؟

  • When you shake your BBC micro:bit, a random phrase is created that consists of an adjective, noun, verb, then adverb, for example, ‘beautiful bird eats swiftly’.
  • You can either use your phrase to start a poem or alongside other phrases generated by this program to write an entire poem.
  • The program uses four arrays called ‘adjectives’, ‘nouns’, ‘verbs’, and ‘adverbs’.
  • Each array stores a list of words. Each item in an array is called an element. The elements are numbered with an index. For example, the array ‘noun’ includes three elements: element 0, bird; element 1, butterfly; and element 2, dragonfly. The indices are numbered from 0 because computers start counting at 0.
  • When you shake your micro:bit, a random number between 0 and 2 is chosen for each array. The elements for those indices are shown on the micro:bit’s LED display.

ما تحتاجه

  • المايكروبيت (micro:bit) (أو محاكي MakeCode)
  • محرر MakeCode
  • حزمة البطارية (اختياري)

الخطوة 2: برمجها

1# Imports go at the top
2from microbit import *
3import random
4
5adjectives = ['beautiful', 
6           'delicate',
7           'bright']
8
9nouns = ['bird', 
10           'butterfly',
11           'dragonfly']
12
13verbs = ['eats', 
14           'flies',
15           'flutters']
16
17adverbs = ['swiftly', 
18           'silently',
19           'skillfully']
20
21while True:
22    if accelerometer.was_gesture('shake'):
23        choice = random.randint(0, len(adjectives)-1)
24        display.scroll(adjectives[choice])        
25        choice = random.randint(0, len(nouns)-1)
26        display.scroll(nouns[choice])
27        choice = random.randint(0, len(verbs)-1)
28        display.scroll(verbs[choice])
29        choice = random.randint(0, len(adverbs)-1)
30        display.scroll(adverbs[choice]) 

الخطوة 3: حسّنها

  • Add more adjectives, nouns, verbs, and adverbs to the program.
  • Find a way to generate more lines for your poem using other inputs such as button A or button B.
  • Can you write a program to generate a haiku?