micro:bit & MicroPython

MicroPython メモ 10 / format() メソッド

参考: Programming the BBC micro:bit: Getting Started with MicroPython, p.67 {} がプレースホルダーとして働く。 ―――――――――――――――――――――― Lua の場合:

MicroPython メモ 9 / ステートマシン / それに意味のないとき

参考: Programming the BBC micro:bit: Getting Started with MicroPython, pp.59-65 from microbit import * state = "A" def handle_A_state(): global state if button_b.is_pressed(): state = "B" display.show(state) def handle_B_state(): global st…

MicroPython メモ 8 / グローバル変数とローカル変数と

参考: Programming the BBC micro:bit: Getting Started with MicroPython, p.62 1 行目の変数 a はグローバル変数であり、4 行目の変数 a はローカル変数であるため、全然別の変数であると見なされる。 函数内で global コマンドを使えば、グローバル変数 a…

MicroPython メモ 7 / 辞書

参考: Programming the BBC micro:bit: Getting Started with MicroPython, p.53-54 辞書はいわゆる聯想配列のことである (Lua はリストも辞書もテーブルとして一緒くたに扱う)。 辞書は { key : value , key : value , ...... } という形でコンストラクトす…

MicroPython メモ 6 / リスト

参考: Programming the BBC micro:bit: Getting Started with MicroPython, p.45-51 リストはいわゆる配列のこと。[ ] で括ってコンストラクトする。インデックスは Lua とは違って普通に 0 から始まる。インデックスの指定は他の言語と同じく [ ] で括る。 …

MicroPython メモ 5 / 函数を定義する

参考: Programming the BBC micro:bit: Getting Started with MicroPython, p.39-44 函数は def 文で定義する (define、definition の意味)。ブロック内で定義した変数は自動的にブロック内のローカル変数と見なされる (Lua は明示的に local 宣言しない限り…

MicroPython メモ 4 / if, elif, else

参考: Programming the BBC micro:bit: Getting Started with MicroPython, p.34-35 「コロン」「インデント」「elif の綴り」に気をつけさえすれば使い方は他の言語と同じ。 from microbit import * while True: if button_a.was_pressed(): display.scroll…

MicroPython メモ 3 / for ループ

参考: Programming the BBC micro:bit: Getting Started with MicroPython, p.33-34 REPL で試してみる。 コロンのあとで改行すると以降は自動的にインデントが設定される。 2 行目を入力して改行したところで BackSpace キーを 1 回押せばそれで自動インデ…

MicroPython メモ 2 / while ループ

参考: Programming the BBC micro:bit: Getting Started with MicroPython, p.31-32 from microbit import * while True: display.scroll("hello") sleep(5000) display.scroll("bye") sleep(5000) インデントはスペース 4 つが一般的。 display.scroll() の…

MicroPython メモ 1

参考: Programming the BBC micro:bit: Getting Started with MicroPython, p.25-31 数値は整数と浮動小数点数の 2 種類。 REPL のプロンプトで上向き矢印をクリックすると過去に入力したコマンドがさかのぼれる。 Lua と違って複合代入演算子 (+= など) が…

hello, world

参考: Programming the BBC micro:bit: Getting Started with MicroPython, p.20 from microbit import * while True: display.scroll("hello, world") display.show(Image.HEART) sleep(2000) 文字列は一重二重のどちらの引用符で括ってもよい。 sleep() の…

初めて電源を入れたとき

参考: Programming the BBC micro:bit: Getting Started with MicroPython, p.3 HELLO. A← →B SHAKE! CHASE THE DOT GREAT! NOW GET CODING!