Skip to content

Atividade

Poetry generator

Intermédio | MakeCode, Python | Acelerómetro, Ecrã LED | Aleatoriedade, Gramática, Poesia

Passo 1: Faz tu mesmo

O que é isto?

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.  

Estes dois vídeos mostram o que vais fazer e como programar:

What will you learn?

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

Como é que funciona

  • 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.

Do que é que precisas

  • micro:bit (ou simulador MakeCode)
  • Editor MakeCode
  • Suporte de pilhas (opcional)

Passo 2: Cria o teu código

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]) 

Passo 3: Melhora-o

  • 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?