Difference between revisions of "CS110:Lab01"

From IPRE Wiki
Jump to: navigation, search
('''Assignment 01''')
(Creating Python Modules)
Line 135: Line 135:
#File: miniPlay.py
#File: miniPlay.py
#Author: Sarah Reddington
#Description: a mini play/conversation between Romeo and Juliet  
#Description: a mini play/conversation between Romeo and Juliet  

Revision as of 12:12, 2 September 2009

Lab 1: Writing Programs, Defining Functions and Creating Modules in Python


  • Write and run programs in Python’s IDLE
  • Become familiar with the Myro Library
  • Write simple functions
  • Create Python Scripts/Modules

Main Concepts and Terminology

  • IDLE, Program, variables, modules

Lab Exercises

Starting IDLE

IDLE which stands for “Interactive DeveLopment Environment” is an environment in which you can write and execute python programs.

To start IDLE:
Double click the “Start Python.pwd” icon on your desktop.

When you first start IDLE, you may see something like this:


The >>> is the Python prompt. It indicates that the computer is waiting for us to give it a command.

Writing Programs

A program is a set of statements or instructions that tell the computer what and how it should execute a task. The following instructions tell the computer to print a statement using the print command:

>>> print “Hello World!”
Hello World!
>>> print “I love python”
I love python
>>> print “My name is Sarah”
My name is Sarah

The statements should look like this in IDLE (notice the color code):


Using Variables

Variables are like containers that store different values that can be looked up or changed at a later time. Below is an example of how variables are defined and used:

>>> x = 3
>>> y = x * x
>>> print y
>>> words = "This is a string of text."
>>> words
'This is a string of text.'
>>> print words
This is a string of text.

In the code example above, x, y and words are all variables. Variables can store any data type. In the previous example, x and y store integers but the variable ‘words’ stores a string (any piece of text enclosed in quotes). You can see the value of the variable by simply writing the name of the variable at the prompt or by typing print ‘variable name’.

Using the Myro Module

Myro, which stands for My Robotics, is a software package developed by the Institute for Personal Robots in Education (IPRE). It is a platform designed to enable beginners to program robots while learning about computer science. It consists of several python modules that you will use to control your personal robots and carry out other tasks. To use the functions in the Myro library type the following:

>>> from myro import *

To see a list of all the functions that are defined in the myro library, type the following:

>>> dir(myro)

Try using the following myro functions:

  • speak("message") - computer will say the text message
  • getVoices() - get the list of available voices
  • setVoice("voice name") - set the voice to a named voice
  • getVoice() - get the name of the current voice
>>> speak("Today is the greatest day of my life")
Today is the greatest day of my life
>>> getVoices()
>>> getVoice()
>>> setVoice('MS-Anna-1033-20-DSK')

Another function to try is:

  • askQuestion(question, [answerList]) - This function prompts a question and returns answer.
askQuestion('Are you ready?',['yes', 'no', 'maybe'])

You should see the following:


Defining Functions

Python allows us to put a sequence of statements together in a function. Functions are defined using the following format:

def functionName(arg1, arg2, ...):

The keyword def is always used at the beginning of a function definition. This is followed by the name of the function and the function’s arguments (listed as arg1, arg2, … above) in parentheses. Arguments are input values that the function needs to perform a task.

Now it's time to create your own functions. Try the following. Feel free to modify the functions to create your own.

>>> def juliet(message):
	setVoice('MS MARY')
>>> def romeo(message):
	setVoice('MS MIKE')

Now that you have defined these functions, you can use them. Here is a mini play starring Romeo and Juliet:

>>> juliet("Hey Ro, where have you been?")
>>> romeo("Hey J, I was chillin")

Creating Python Modules

One problem with defining functions interactively at the Python prompt like this is that the definitions go away when we quit Python. If we want to use them again in the future, we have to retype them. Python allows you to create modules or files that contain your function definitions and other commands. This file is saved on a disk so that it can be used over and over again.

A module is just a text file that you can create in any editor such as Notepad and Microsoft Word (as long as you save your program as a ‘plain text’ file). Python files must be saved with a ‘.py’ extension so that the computer knows it is a python module. Let’s illustrate the use of a module by creating our own and running it.

You can open up python’s editor in IDLE by going to File>>New Window. Copy and paste the functions you have written into a new file and save it. You should add the '.py' extension to your file name. This tells the computer that it is a python file. Your module should look something like this:

#File: miniPlay.py
#Author: Sarah Reddington
#Description: a mini play/conversation between Romeo and Juliet 

from myro import *

def juliet(message):
    setVoice('MS MARY')

def romeo(message):
    setVoice('MS MIKE')

# Calling the functions:

juliet("Hey Ro, where have you been?")
romeo("Hey J, I was chillin")

Running a Python Module

To run a python module, go to Run>>>Run Module in your menu options as shown below:


Assignment 01

  1. Submit Your Module: Print out a copy of the module you wrote and submit it to me by the end of class. If you are not done by the end of class, you can leave a copy of this module in the bin outside my office (Park 249). Please submit it by 5 pm of your lab day. Also, please include your name, lab section and time, and date of submission on this page.
  2. Initial Reactions: How did you find the first lab? Was it manageable or overwhelming? You can write or type your initial reactions at the end of your module.
  3. Readings: Read chapter 1 (“The World of Robots") of your text (Learning Computing with Robots). You will need to be familiar with this chapter in order to do lab 2.