跳轉到內容

使用 Linkbot 學習 Python 3 / 誰在那裡?

來自華夏公益教科書,開放的書籍,開放的世界
Lesson Information
**To Be Added**
Vocabulary:
Necessary Materials and Resources:
Computer Science Teachers Association Standards: 5.3.A.CD.4: Compare various forms of input and output.
Common Core Math Content Standards:
Common Core Math Practice Standards:
Common Core English Language Arts Standards:


輸入和變數

[編輯 | 編輯原始碼]

現在,您已準備好建立更復雜的程式。在您的 Python 編輯器中輸入以下內容:

print("Halt!")
user_input = input("Who goes there?")
print("You may pass,",  user_input)

執行您的程式,確保它顯示以下內容:

Halt!
Who goes there? Linkbot
You may pass, Linkbot

注意:在透過按鍵盤上的 F5 執行程式碼後,Python Shell 僅會輸出

Halt!
Who goes there?

在 Python Shell 中輸入您的姓名並按回車鍵以獲得剩餘的輸出。

您的程式將由於 input() 語句而有所不同。請注意,在執行程式時,您必須輸入您的姓名並按“Enter”鍵。程式將透過顯示包含您姓名的文字進行響應。這是一個關於 _輸入_ 的示例。程式到達某個點後,會等待使用者輸入更多資料以便程式將其包含在內。

程式包含的使用者資料儲存在一個 _變數_ 中。在前面的程式中,user_input 是一個 _變數_。變數就像一個可以儲存資料片段的盒子。該程式演示了變數的使用

(有用的簡短說明,Aaron,“這可能現在沒有太大意義,但在您閱讀關於變數的段落時,請參考它。”)

a = 123.4
b23 = 'Barobo'
first_name = "Linkbot"
b = 432
c = a + b
print("a + b is",c)
print("first_name is",first_name)
print("The Linkbot was created by ",b23)

這是輸出

a + b is 555.4
first_name is Linkbot
The Linkbot was created by Barobo

變數儲存資料,例如數字、單詞、短語或您選擇讓它儲存的任何內容。上面程式中的變數是 ab23first_namebc。兩種基本型別的變數是 _數字_ 和 _字串_。_數字_ 只是一個數學上的數值,例如 7 或 89 或 -3.14。_ 在上面的示例中,ab,甚至 c 都是數字變數。c 被認為是數字變數,因為結果是一個數字。_字串_ 是字母、數字和其他字元的序列。_ 在此示例中,b23first_name 是儲存字串的變數。BaroboLinkbota + b isfirst_name isThe Linkbot was created by 是此程式中的字串。這些字元用 "' 括起來。程式中使用的其他符號是表示數字的變數。請記住,變數用於儲存值,它們不使用標點符號("')。如果您想使用實際的 _值_,則 _必須_ 使用這些標點符號。請參閱下一個示例。

value1 == Pim
value2 == "Pim"

這兩行程式碼乍一看似乎相同,但它們是不同的。在第一個程式碼中,Python 檢查儲存在變數 value1 中的值是否與儲存在 _變數_ Pim 中的值相同。在第二個程式碼中,Python 檢查字串(實際的字母 Pim)是否與 value2 中的字串相同(關於字串和 == 的更多解釋將在稍後給出)。

回顧一下,有一些稱為變數的盒子,資料會進入這些盒子。回想一下前面的示例。計算機看到一行程式碼,例如 first_name = "Linkbot",它會將其解讀為“將字串 Linkbot 放入盒子或變數 first_name 中”。然後,計算機看到語句 c = a + b,它會將其解讀為“將 a + b123.4 + 432(等於 555.4)的總和放入 c 中”。語句 a + b 的右側被 _計算_,這意味著這兩個項相等,結果儲存在左側的變數 c 中。換句話說,語句的一側被 _分配_ 給另一側。這個過程稱為 _賦值_。

程式碼中的單個等號使該行程式碼成為一個語法語句。例如,apple = banana 表示“apple 等於 banana”。透過以這種方式編寫程式碼,apple 被分配給 banana,並且在程式中它們被認為相等。您實際上是在告訴程式這兩個項相等,即使實際上並非如此。

兩個等號放在一起使該行程式碼成為一個計算機將回答的問題。例如,apple == banana 表示“apple 等於 banana 嗎?”當然,答案應該是“否”,除非您之前錯誤地將 apple 分配給了 banana。

(有用的簡短說明,Aaron,“請注意您使用的 = 的數量,因為這會徹底改變您的程式!”)

這是另一個變數使用示例

a = 1
print(a)
a = a + 1
print(a)
a = a * 2
print(a)

這是輸出

1
2
4

即使同一個變量出現在等號的兩側(例如,spam = spam),計算機仍然將其解讀為“首先找出要儲存的資料,然後找出資料的儲存位置”。

嘗試另一個程式

number = float(input("Type in a number: "))
integer = int(input("Type in an integer: "))
text = input("Type in a string: ")
print("number =", number)
print("number is a", type(number))
print("number * 2 =", number * 2)
print("integer =", integer)
print("integer is a", type(integer))
print("integer * 2 =", integer * 2)
print("text =", text)
print("text is a", type(text))
print("text * 2 =", text * 2)

輸出應為

Type in a number: 12.34
Type in an integer: -3
Type in a string: Hello
number = 12.34
number is a <class 'float'>
number * 2 = 24.68
integer = -3
integer is a <class 'int'>
integer * 2 = -6
text = Hello
text is a <class 'str'>
text * 2 = HelloHello

請注意,number 是用 float(input()) 建立的,而 text 是用 input() 建立的。input() 返回一個字串,而函式 float 從字串返回一個數字。int 返回一個整數,即沒有小數點的數字。當您希望使用者輸入小數時,請使用 float(input());如果您希望使用者輸入整數,請使用 int(input());但如果您希望使用者輸入字串,請使用 input()

程式的後半部分使用 type() 函式來識別變數的型別。數字的型別為 intfloat,分別代表 _整數_ 和 _浮點數_(主要用於小數)。文字字串的型別為 str,代表 _字串_。整數和浮點數可以由數學函式進行運算,字串則不能。請注意,當 Python 將數字乘以整數時,會發生預期的事情。但是,當字串乘以整數時,結果是生成字串的多個副本(即,text * 2 = HelloHello)。

字串操作與數字操作不同。此外,某些操作只適用於數字(整數和浮點數),如果使用字串,則會引發錯誤。以下是一些互動模式示例,說明這一點。

>>> print("This" + " " + "is" + " joined.")
This is joined.
>>> print("Ha, " * 5)
Ha, Ha, Ha, Ha, Ha, 
>>> print("Ha, " * 5 + "ha!")
Ha, Ha, Ha, Ha, Ha, ha!
>>> print(3 - 1)
2
>>> print("3" - "1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>> 

以下是部分字串操作的列表

操作 符號 示例
重複 * "i" * 5 == "iiiii"
連線 + "Hello, " + "World!" == "Hello, World!"

移動 Linkbot 的電機

[編輯 | 編輯原始碼]

每個 Linkbot 都有兩個關節,它可以移動這些關節來執行各種任務。有時,關節上可能連線有輪子,這樣 Linkbot 就可以像一輛雙輪車一樣四處行駛。或者,關節上可能連線著一個夾鉗,這將允許 Linkbot 抓取物體。

Linkbot 上的每個關節都印有數字。從“頂向下”的角度直接觀察 Linkbot,關節 1 在左側,關節 2 朝向您,關節 3 在右側。Linkbot-L 只有關節 1 和 2,而 Linkbot-I 只有關節 1 和 3。讓我們學習如何移動 Linkbot 上的關節!

moveMotor.py

# This program moves Joint 1 on a Linkbot by an amount 
# specified by the user
import barobo
myDongle = barobo.Dongle()
myDongle.connect()
robotID = input('Enter Linkbot ID: ')
myLinkbot = myDongle.getLinkbot(robotID)

degrees = float(input("Degrees to rotate Joint 1: "))
myLinkbot.moveJoint(1, degrees)

當您執行此示例時,您應該會注意到關節 1 移動。如果您輸入“90”,您應該會看到關節逆時針旋轉 90 度。如果您輸入“-90”,您應該會看到關節順時針旋轉 90 度。在執行此程式時,請注意輸入非常大的數字,否則您可能需要等待一段時間才能使關節停止移動。

Rate_times.py

# This program calculates rate and distance problems
print("Input a rate and a distance")
rate= float(input("rate: "))
distance = float(input("Distance: "))
print("Time:", (distance / rate))

示例執行

Input a rate and a distance
Rate: 5
Distance: 10
Time: 2.0
Input a rate and a distance
Rate: 3.52
Distance: 45.6
Time: 12.9545454545

Area.py

# This program calculates the perimeter and area of a rectangle
print("Calculate information about a rectangle")
length = float(input("Length: "))
width = float(input("Width: "))
print("Area:", length * width)
print("Perimeter:", 2 * length + 2 * width)

示例執行

Calculate information about a rectangle
Length: 4
Width: 3
Area: 12.0
Perimeter: 14.0
Calculate information about a rectangle
Length: 2.53
Width: 5.2
Area: 13.156
Perimeter: 15.46

Temperature.py

# This program converts Fahrenheit to Celsius
fahr_temp = float(input("Fahrenheit temperature: "))
print("Celsius temperature:", (fahr_temp - 32.0) * 5.0 / 9.0)

示例執行

Fahrenheit temperature: 32
Celsius temperature: 0.0
Fahrenheit temperature: -40
Celsius temperature: -40.0
Fahrenheit temperature: 212
Celsius temperature: 100.0
Fahrenheit temperature: 98.6
Celsius temperature: 37.0


編寫一個程式,從使用者那裡獲取兩個字串變數和兩個數字變數,將它們連線在一起(不留空格)並顯示字串,然後在新行上將兩個數字相乘。

解決方案

編寫一個程式,從使用者那裡獲取兩個字串變數和兩個數字變數,將它們連線在一起(不留空格)並顯示字串,然後在新行上將兩個數字相乘。

  
string1 = input('String 1: ')
string2 = input('String 2: ')
float1 = float(input('Number 1: '))
float2 = float(input('Number 2: '))
print(string1 + string2)
print(float1 * float2)


編寫一個程式,從使用者那裡獲取兩個數字變數;每個變數都是以度為單位的角度。程式獲取這兩個數字後,它使 Linkbot 的關節 1 旋轉第一個數字指定的量,然後使關節 3(或者如果你是 Linkbot-L 則為關節 2)旋轉第二個數字指定的量。

解決方案
# This program moves Joints 1 and 3 on a Linkbot by an amount 
# specified by the user
import barobo
myDongle = barobo.Dongle()
myDongle.connect()
robotID = input('Enter Linkbot ID: ')
myLinkbot = myDongle.getLinkbot(robotID)

degrees1 = float(input("Degrees to rotate Joint 1: "))
degrees3 = float(input("Degrees to rotate Joint 3: "))
myLinkbot.moveJoint(1, degrees1)
myLinkbot.moveJoint(3, degrees3)


使用 Linkbot 學習 Python 3
 ← 你好,世界 誰在那裡? 數到 10 → 
華夏公益教科書