使用 Linkbot 學習 Python 3 / Hello, World
Lesson Information **To Be Added** Vocabulary: Python Editor: a program used to create a block of text that will be sent to Python and interpreted. In this book we recommend IDLE 3. Line: a single sentence of programming code. Lines of code can be used to measure the size of a program and compare it to other programs. Function: a type of procedure or routine, a set of instructions to follow. Argument: data provided to a function as input. This is like the domain in algebra. String: a sequence of characters representing either a constant or a variable. Function Call: Necessary Materials and Resources: Computer Science Teachers Association Standards: L1:3.CT.4:Recognize that software is created to control computer operations Common Core Math Practice Standards: CCSS.MATH.PRACTICE.MP5 Use appropriate tools strategically.
完成本章後,你將瞭解如何在 Python 編輯器、IDLE 3 或其他文字編輯器中編輯程式、儲存程式並執行程式。
從程式設計教程開始,"Hello World!"[1] 已經成為在螢幕上建立內容的第一個嘗試,以下是如何使用 Python 來實現。
print("Hello, World!")
如果你使用命令列執行此程式,則在文字編輯器中輸入它,將其儲存為 hello.py,然後使用 python3.0 hello.py 執行它。
否則,進入你的 Python 編輯器,建立一個新檔案(在 IDLE 中按 Ctrl + N),並像在 建立和執行程式 節中一樣建立程式。
執行此程式時,它會列印以下內容
Hello, World!
記住,每當你看到程式碼作為示例時,最好將其輸入並執行它。透過實踐學習可能是一種強大的工具!這值得花費時間。
以下是一個使用 Linkbot 說“Hello World”的修改版。添加了註釋以幫助你逐步理解程式碼的作用。
import barobo #loads 'barobo' module
import time #loads 'time' module
dongle = barobo.Dongle() #uses commands from 'barobo' to connect Linkbot
dongle.connect()
robotID = input('Enter Linkbot ID: ') #prompts user for Linkbot ID
robot = dongle.getLinkbot(robotID)
print ( 'Hello Linkbot Programmer' ) #prints greeting
robot.setBuzzerFrequency(1047) #calls for pizo buzzer to make a tone
time.sleep(0.25) #sets duration of tone
robot.setBuzzerFrequency(1567) #changes tone
time.sleep(0.5)
robot.setBuzzerFrequency(0) #turns off tone.
可以隨意更改蜂鳴器的頻率來改變音調,以及更改 time.sleep 的持續時間,看看它對問候語有什麼影響。
現在,這裡還有幾個程式
print("I pledge allegiance to the flag")
print("of the United States of America,")
print("and to the republic, for which it stands,")
執行此程式時,它會打印出
I pledge allegiance to the flag of the United States of America, and to the republic, for which it stands,
當計算機執行此程式時,它首先看到 行(像一行程式碼)
print("I pledge allegiance to the flag")
所以計算機列印
I pledge allegiance to the flag
然後計算機向下移動到下一行,看到
print("of the United States of America,")
所以計算機列印到螢幕上
of the United States of America,
計算機繼續檢視每一行,遵循命令,然後繼續執行下一行。計算機不斷執行命令,直到它到達程式的末尾。
Linkbot 配備了多色 LED,它本質上只是一個可以改變顏色的燈光。讓我們嘗試一個簡單的程式來控制 Linkbot 上的 LED 顏色。要開始,請按照以下步驟操作
- 按下電源按鈕,直到 Linkbot 閃爍明亮的紅光,從而開啟 Linkbot。
- 大約 5 秒後,Linkbot 會發出嗶嗶聲並顯示藍光。現在你的 Linkbot 已開啟!
- 取一根 Micro-USB 線並將 Linkbot 連線到你的電腦。
- 開啟你的文字編輯器並嘗試以下程式碼:https://wikibook.tw/wiki/Learning_Python_3_with_the_Barobo_Linkbot
import barobo
myDongle = barobo.Dongle()
myDongle.connect()
myLinkbot = myDongle.getLinkbot()
myLinkbot.setLEDColor(0, 255, 0)
注意 如果不太理解此程式前四行中的內容,也不用擔心。所有這些概念將在後面的章節中介紹。現在,只需知道,如果你想控制 Barobo Linkbot,這四行(或類似的行)是必要的。 |
執行此程式時,你應該會注意到 Linkbot 變成了綠色!在你編寫的程式碼中,有三個數字:0、255 和 0。這三個數字決定了 Linkbot 內部紅、綠、藍 LED 的亮度,其中 0 表示最低亮度設定,255 表示最大亮度設定。如果你想嘗試讓 Linkbot 變成亮紫色,可以嘗試將顏色數字設定為“255, 0, 255”。這告訴 Linkbot 將其紅色和藍色 LED 開啟到最大亮度。當紅光與藍光混合時,它會呈現紫色!
現在可能是給你解釋一下正在發生的事情,以及一些程式設計術語的好時機。
我們上面所做的是使用名為 print 的 函式。函式名稱 - print - 後面跟著包含零個或多個 引數 的括號。因此,在此示例中
print("Hello, World!")
有一個引數,即 "Hello, World!"。注意,此引數是一組包含在雙引號("")中的字元。這通常稱為字元字串,簡稱 字串。另一個字串示例是 "Jack and Jill went up a hill"。函式和包含引數的括號的組合稱為 函式呼叫。
函式及其引數是 Python 中的一種語句型別,因此
print("Hello, World!")
是一個語句示例。基本上,你可以將語句視為程式中的一行程式碼。
現在這些術語可能已經足夠多了。
以下另一個程式
print("2 + 2 is", 2 + 2)
print("3 * 4 is", 3 * 4)
print("100 - 1 is", 100 - 1)
print("(33 + 2) / 5 + 11.5 is", (33 + 2) / 5 + 11.5)
這是程式執行時的 輸出
2 + 2 is 4 3 * 4 is 12 100 - 1 is 99 (33 + 2) / 5 + 11.5 is 18.5
如你所見,Python 可以將價值數千美元的計算機變成價值 5 美元的計算器。
在此示例中,print 函式後跟兩個引數,每個引數之間用逗號隔開。因此,對於程式的第一行
print("2 + 2 is", 2 + 2)
第一個引數是字串 "2 + 2 is",第二個引數是 數學表示式 2 + 2,通常稱為 表示式。
需要注意的是,字串按原樣列印(不帶包含的雙引號),但 表示式 會被 評估 或轉換為實際值。
Python 對數字有七種基本運算
| 運算 | 符號 | 示例 |
|---|---|---|
| 冪(指數) | **
|
5 ** 2 == 25
|
| 乘法 | *
|
2 * 3 == 6
|
| 除法 | /
|
14 / 3 == 4.666666666666667
|
| 整數除法 | //
|
14 // 3 == 4
|
| 餘數(模運算) | %
|
14 % 3 == 2
|
| 加法 | +
|
1 + 2 == 3
|
| 減法 | -
|
4 - 3 == 1
|
注意,除法有兩種方式,一種返回迴圈小數,另一種可以獲取餘數和整數。運算順序與數學中的相同
- 括號
() - 指數
** - 乘法
*、除法/、整數除法//和餘數% - 加法
+和減法-
因此,在需要時使用括號來構建公式。
在程式設計中,你經常會做一些複雜的事情,並且可能在將來忘記自己做了什麼。在這種情況下,程式可能需要添加註釋。註釋 是對你自己和其他程式設計師的說明,解釋正在發生的事情。例如
# Not quite PI, but a credible simulation
print(22 / 7)
其輸出為
3.14285714286
注意,註釋以井號開頭:#。註釋用於與閱讀程式的人員以及將來的自己進行交流,以清楚地說明覆雜的部分。
注意,註釋後可以是任何文字,並且當程式執行時,從 # 到該行末尾的文字會被忽略。# 不必位於新行的開頭
# Output PI on the screen
print(22 / 7) # Well, just a good approximation
每一章(最終)將包含本章中介紹的程式設計特性的示例。你至少應該瀏覽一下它們,看看是否理解。如果不理解,可以嘗試將其輸入並看看會發生什麼。隨意修改它們,看看會發生什麼。
Denmark.py
print("Something's rotten in the state of Denmark.")
print(" -- Shakespeare")
輸出
Something's rotten in the state of Denmark.
-- Shakespeare
School.py
# This is not quite true outside of USA
# and is based on my dim memories of my younger years
print("Firstish Grade")
print("1 + 1 =", 1 + 1)
print("2 + 4 =", 2 + 4)
print("5 - 2 =", 5 - 2)
print()
print("Thirdish Grade")
print("243 - 23 =", 243 - 23)
print("12 * 4 =", 12 * 4)
print("12 / 3 =", 12 / 3)
print("13 / 3 =", 13 // 3, "R", 13 % 3)
print()
print("Junior High")
print("123.56 - 62.12 =", 123.56 - 62.12)
print("(4 + 3) * 2 =", (4 + 3) * 2)
print("4 + 3 * 2 =", 4 + 3 * 2)
print("3 ** 2 =", 3 ** 2)
輸出
Firstish Grade 1 + 1 = 2 2 + 4 = 6 5 - 2 = 3 Thirdish Grade 243 - 23 = 220 12 * 4 = 48 12 / 3 = 4 13 / 3 = 4 R 1 Junior High 123.56 - 62.12 = 61.44 (4 + 3) * 2 = 14 4 + 3 * 2 = 10 3 ** 2 = 9
- 編寫一個程式,分別以字串形式列印你的全名和生日。
- 編寫一個程式,展示所有 7 個數學函式的使用。
1. 編寫一個程式,分別以字串形式列印你的全名和生日。
print("Ada Lovelace", "born on", "November 27, 1852")
print("Albert Einstein", "born on", "14 March 1879")
print(("John Smith"), ("born on"), ("14 March 1879"))
2. 編寫一個程式,展示所有 7 個數學函式的使用。
print("5**5 = ", 5**5)
print("6*7 = ", 6*7)
print("56/8 = ", 56/8)
print("14//6 = ", 14//6)
print("14%6 = ", 14%6)
print("5+6 = ", 5+6)
print("9-0 = ", 9-0)