Learning Python 3 with the Linkbot/Using Modules

Here's this chapter's typing exercise (name it cal.py (import actually looks for a file named calendar.py and reads it in. If the file is named calendar.py and it sees a "import calendar" it tries to read in itself which works poorly at best.)):

importcalendaryear=int(input("Type in the year number: "))calendar.prcal(year)

(I skipped some of the output, but I think you get the idea.) So what does the program do? The first line import calendar uses a new command import. The command import loads a module (in this case the calendar module). To see the commands available in the standard modules either look in the library reference for python (if you downloaded it) or go to http://docs.python.org/library/. If you look at the documentation for the calendar module, it lists a function called prcal that prints a calendar for a year. The line calendar.prcal(year) uses this function. In summary to use a module import it and then use module_name.function for functions in the module. Another way to write the program is:

fromcalendarimportprcalyear=int(input("Type in the year number: "))prcal(year)

This version imports a specific function from a module. Here is another program that uses the Python Library (name it something like clock.py) (press Ctrl and the 'c' key at the same time to terminate the program):

fromtimeimporttime,ctimeprev_time=""whileTrue:the_time=ctime(time())ifprev_time!=the_time:print("The time is:",ctime(time()))prev_time=the_time

The output is infinite of course so I canceled it (or the output at least continues until Ctrl+C is pressed). The program just does a infinite loop (True is always true, so while True: goes forever) and each time checks to see if the time has changed and prints it if it has. Notice how multiple names after the import statement are used in the line from time import time, ctime.

The Python Library contains many useful functions. These functions give your programs more abilities and many of them can simplify programming in Python.

The following Barobo Linkbot program demonstrates multiple modules that are used to create some fun examples of playing with the Linkbot functionality. Comments have been added to the program to explain what the link is doing as you read. Once again the while True: puts the program in an infinite loop that can be exited when Ctrl+C is pressed.

importbarobo#imports the module with the Linkbot commandsimporttime#imports the module to use time.sleepimportrandom#imports a random integer generatordongle=barobo.Dongle()#this block uses the 'barobo' module to connect the Linkbotdongle.connect()#and the dongle that is plugged into the computerrobotID=input('Enter robot ID: ')#prompts user to enter Linkbot IDrobot=dongle.getLinkbot(robotID)defcallback(mask,buttons,data):ifbuttons&0x02:robot.setLEDColor(#calls setLEDColor from 'barobo' modulerandom.randint(0,255),#uses the 'random' integer generator fromrandom.randint(0,255),#the random module to set colorsrandom.randint(0,255))robot.enableButtonCallback(callback)whileTrue:time.sleep(1)#uses the 'time' module to call time.sleep command

Check out the range of colors and intensity the Linkbot is capable of generating with its built in RGB LED.

Rewrite the high_low.py program from section Decisions to use an random integer between 0 and 99 instead of the hard-coded 78. Use the Python documentation to find an appropriate module and function to do this.

Solution

Rewrite the high_low.py program from section Decisions to use an random integer between 0 and 99 instead of the hard-coded 78. Use the Python documentation to find an appropriate module and function to do this.