使用 Linkbot 學習 Python 3 / 使用模組
這是本章的打字練習(將其命名為 cal.py(import 實際上會尋找名為 calendar.py 的檔案並讀取它。如果檔名為 calendar.py 並且它看到一個 "import calendar",它會嘗試讀取自身,這在最佳情況下也難以正常工作。))
import calendar
year = int(input("Type in the year number: "))
calendar.prcal(year)
以下是我的部分輸出
Type in the year number: 2001
2001
January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7 1 2 3 4 1 2 3 4
8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 11
15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 18
22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 25
29 30 31 26 27 28 26 27 28 29 30 31
(我跳過了部分輸出,但我認為你明白了吧。) 那麼程式做了什麼?第一行 import calendar 使用了一個新的命令 import。命令 import 載入了一個模組(在本例中是 calendar 模組)。要檢視標準模組中可用的命令,請檢視 Python 庫參考(如果你下載了它)或訪問 https://docs.python.club.tw/library/。如果你檢視 calendar 模組的文件,它列出了一個名為 prcal 的函式,該函式列印一年日曆。行 calendar.prcal(year) 使用了此函式。總結一下,要使用模組,import 它,然後使用 module_name.function 來呼叫模組中的函式。另一種編寫程式的方式是
from calendar import prcal
year = int(input("Type in the year number: "))
prcal(year)
此版本從模組中匯入特定函式。這裡還有另一個使用 Python 庫的程式(將其命名為類似 clock.py 的名稱)(同時按 Ctrl 和 'c' 鍵以終止程式)
from time import time, ctime
prev_time = ""
while True:
the_time = ctime(time())
if prev_time != the_time:
print("The time is:", ctime(time()))
prev_time = the_time
部分輸出為
The time is: Sun Aug 20 13:40:04 2000
The time is: Sun Aug 20 13:40:05 2000
The time is: Sun Aug 20 13:40:06 2000
The time is: Sun Aug 20 13:40:07 2000
Traceback (innermost last):
File "clock.py", line 5, in ?
the_time = ctime(time())
KeyboardInterrupt
輸出當然是無限的,所以我取消了它(或者至少輸出會一直持續到按下 Ctrl+C)。程式只是做了一個無限迴圈(True 永遠為真,所以 while True: 會永遠執行),並且每次都會檢查時間是否改變,如果改變了就列印它。請注意,在 from time import time, ctime 行中如何使用 import 語句後的多個名稱。
Python 庫包含許多有用的函式。這些函式為你的程式提供了更多功能,其中許多函式可以簡化 Python 中的程式設計。
以下 Barobo Linkbot 程式演示了多個模組,這些模組用於建立一些有趣的例子,來演示如何使用 Linkbot 功能。程式中添加了註釋來解釋 Linkbot 的操作,方便你閱讀。同樣,while True: 將程式置於一個無限迴圈中,可以透過按下 Ctrl+C 退出。
import barobo #imports the module with the Linkbot commands
import time #imports the module to use time.sleep
import random #imports a random integer generator
dongle = barobo.Dongle() #this block uses the 'barobo' module to connect the Linkbot
dongle.connect() #and the dongle that is plugged into the computer
robotID = input('Enter robot ID: ') #prompts user to enter Linkbot ID
robot = dongle.getLinkbot(robotID)
def callback(mask, buttons, data):
if buttons & 0x02:
robot.setLEDColor( #calls setLEDColor from 'barobo' module
random.randint(0, 255), #uses the 'random' integer generator from
random.randint(0, 255), #the random module to set colors
random.randint(0, 255)
)
robot.enableButtonCallback(callback)
while True:
time.sleep(1) #uses the 'time' module to call time.sleep command
看看 Linkbot 用內建的 RGB LED 能夠生成的各種顏色和亮度。
將 決策 部分的 high_low.py 程式改寫為使用 0 到 99 之間的隨機整數,而不是硬編碼的 78。使用 Python 文件查詢合適的模組和函式來完成此操作。
將 決策 部分的 high_low.py 程式改寫為使用 0 到 99 之間的隨機整數,而不是硬編碼的 78。使用 Python 文件查詢合適的模組和函式來完成此操作。
from random import randint
number = randint(0, 99)
guess = -1
while guess != number:
guess = int(input ("Guess a number: "))
if guess > number:
print("Too high")
elif guess < number:
print("Too low")
print("Just right")