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 편집기
- AAA 배터리 팩 (옵션)
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?
This content is published under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) licence.