ev3编程 python_乐高 EV3 高级编程 - 第四课:Python 模块
譯者按:使用 ev3dev Linux 系統(tǒng)并用 Python 編程的人數(shù)比例很低,好像這一課這樣寫(xiě) Python 編程的就更少了,你會(huì)發(fā)現(xiàn)程序的重用率會(huì)大大的提高!
EV3: Lesson 4 - Python Modules
EV3:第 4 課 - Python 模塊
4.1 Separate Functions, Main Program and Global Variables
4.1 分開(kāi)的函數(shù),主程序,和全局變量
In our last lesson, we learned how to use functions to perform repeated jobs. However, if the main program contains too many functions, the program size will become too large to be maintained. So, we'll need some technique to separate those functions with the main program.
在我們的上一課,我們學(xué)習(xí)了如何使用函數(shù)去執(zhí)行一些重復(fù)的工作。但是,如果主程序里有太多的函數(shù),程序會(huì)變得太大而難以維護(hù)。所以,我們需要一些技巧去分開(kāi)主程序和函數(shù)。
Those python program files that stored functions are called "Python Modules".
我們叫那些儲(chǔ)存函數(shù)的檔案為【Python 模塊】。
In order to share variables between different python programs and modules, we'll also need to put the global variables to a seperate python module. (Note: global variables can only be used within the same python program)
為了可以讓一些變量可以同時(shí)被主程序及函數(shù)使用,我們也需要把這些全局變量放到一個(gè)分開(kāi)的 Python 模塊去聲明。(要注意的是,前面課程提到的全局變量,是只能在同一個(gè)程序檔案里使用,這一課教的方法,讓全局變量可以跨檔案使用)
The following python programs and modules perform the same job that is described in lesson 3:
下面的幾個(gè) Python 程序,會(huì)執(zhí)行和第 3 課一樣的工作:
To acheive this purpose, let's create an empty python file called "g.py" that stores all required global variables:
要達(dá)到這個(gè)目的,讓我們創(chuàng)建一個(gè)空的 Python 程序,并命名為“g.py”,這個(gè)模塊儲(chǔ)存里所有需要的全局變量:
global intResult
intResult = 0
Then, we put all the functions into another python file called "fun.py"
然后,我們把所有的函數(shù)放進(jìn)去另外一個(gè) Python 模塊“fun.py”
# import g.py so that the variables defined in it can be used in this module
import g
def funAddWithReturn(intNo1, intNo2):
intTemp = intNo1 + intNo2
return intTemp
def funAddWithoutReturn(intNo1, intNo2):
# Please note that intResult has been changed to g.intResult
g.intResult = intNo1 + intNo2
def funAddWithoutReturnWrong(intNo1, intNo2):
intResult = intNo1 + intNo2
Then, we copy other contents from lesson3_03.py to lesson4_01.py with some modifications:
然后,我們把其他程序內(nèi)容從 lesson3_03.py 拷貝到 lesson4_01.py 里,并稍微修改:
#!/usr/bin/env python3
from ev3dev.ev3 import *
from time import sleep, time
import traceback
# import g.py and fun.py
import g
import fun
try:
#The main program starts here
lcd = Screen()
lcd.clear()
# Declare local variable
intA = 3
intB = 5
# The answer for the following statement is 8
# Important Note ********** "fun." is added before the function name, as it is defined there!!! **********
lcd.draw.text((5, 5), "intA + intB = " + str(fun.funAddWithReturn(intA, intB)))
# The answer for the following statement is 0, pls. note that intResult has been changed to g.intResult
fun.funAddWithoutReturnWrong(intA, intB)
lcd.draw.text((5, 20), "intA + intB = " + str(g.intResult))
# The answer for the following statement is 8, pls. note that intResult has been changed to g.intResult
fun.funAddWithoutReturn(intA, intB)
lcd.draw.text((5, 35), "intA + intB = " + str(g.intResult))
lcd.update()
sleep(10)
except:
# If there is any error, it will be stored in the log file in the same directory
logtime = str(time())
f=open("log" + logtime + ".txt",'a')
traceback.print_exc(file=f)
f.flush()
f.close()
Upload the above 3 programs and run lesson4_01.py in the EV3 brick, you'll see exactly the same result as in lesson 3:
把上面的 3 個(gè)程序上傳到 EV3,并且運(yùn)行主程序 lesson4_01.py,你會(huì)發(fā)現(xiàn)屏幕輸出會(huì)和第 3 課的一模一樣:
Figure 4.1
In our next lesson, we'll learn how to control the buttons on the EV3 brick.
在下一課,我們會(huì)學(xué)習(xí)如何控制 EV3 主機(jī)上面的按鍵。
總結(jié)
以上是生活随笔為你收集整理的ev3编程 python_乐高 EV3 高级编程 - 第四课:Python 模块的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 自动驾驶汽车电子电气架构技术开发
- 下一篇: python与乐高ev3结合_利用pyt