Lab 1
This lab has two parts. Part A is intended to help you setup and/or familiarise yourself with the computing environment which you will be using throughout the course. If something unexpected happens, or if you get stuck, you can ask your tutors, or another student.
Part B introduces your first programming exercises.
Both parts should be completed during semester week 2. It is not expected that you will be able to finish all the programming exercises during the scheduled 2 hour lab session. The normal workload for a 6-unit course is a bit over 10 hours per week. That means students in this course are expected to spend at least 5-6 hours per week practicing programming in addition to the scheduled lecture and lab times. We recommend reading through and attempting at least some of the exercises _before _ your lab, since this will allow you to come to the lab prepared with questions to ask the tutors, and therefore to make better use of the lab time.
Part A - Labs operation, getting a working python environment and getting used to it#
Labs operation and environment setup#
Before you program, you need an appropriate python environment. The environment you will be using will differ depending on whether you are in an online lab or in an on-campus lab.
To get started, please read the information on the main labs page here regarding software, and the section appropriate for the type of lab you are in (online or on-campus). If you have not read the general information about labs at the top of the page please do so as well.
Make sure you follow through the various links on the page when reading through! Just so you don’t accidentally miss anything, here’s a list of the main links you should have read through to check with:
For students in online labs:
- Python version
- Online labs info
- Microsoft Teams setup and info (specfically, the labs section)
- Installing python
For students in on-campus labs:
- Python version
- On-campus lab info
- Lab computers guide
- (Strongly recommended, do this on your own PC) Installing python
Regardless of if you are attending labs online or on-campus, we strongly recommend you install python on your own machine. A guide to do so is available here.
Note on organising your files and folders#
How you organise your files on your personal computer is up to you.
However, please be aware than if you want to import python files, they
will need to be in the same directory python is running from. We recommend
creating a folder called “COMP1730
” and then putting a separate folder for
each lab inside it, so a “lab1
” folder, a “lab2
” folder, etc. Aside from
helping you organise your work, this will also assist you in making sure
that python can find the files you are trying to work with.
The python shell#
The python shell (or interpreter) is the program that executes
python programs and interactive commands. It uses the ipython
shell,
which looks a bit different from the standard python3
shell, but under
the hood they are mostly the same. When you start the ipython
shell
you should see a message like this
Python 3.6.8 |Anaconda custom (64-bit)| (default, Dec 30 2018, 01:22:34)
Type "copyright", "credits" or "license" for more information.
IPython 7.5.0 -- An enhanced Interactive Python.
In [1]:
The Python version number may be different in different environments.
For example, on the InfoCommons computers it may be Python 3.6.1
.
The important thing is that it is python 3.
The prompt In [1]:
indicates that the interpreter is waiting for you
to type in a command or expression. (The number in brackets is a running
counter of inputs and outputs.)
If you start a standard python3
shell, the prompt will instead look
like this:
>>>
Examples on these lab pages will be written using the ipython
shell
prompt. There are a few other differences between the two shells: ipython
gives you some shortcuts to make typing in commands interactively easier,
and prefixes the values that results from evaluating an expression with
Out
. When you use certain types of graphics (such as function plots),
the ipython
shell will show them inline in the shell window, while the
python3
shell will show them in a separate window.
The Spyder IDE#
Spyder is the IDE that comes with Anaconda. It is available in both the CSIT lab environment and on the InfoCommons computers. You can find Spyder in the start menu of CSIT lab computers, and in the Windows start menu on the InfoCommons computers.
The Spyder IDE provides you with an editor (to the left in the image below), a python interpreter (below on the right). There is also an area (top right, bounded by the violet box in the image below) which can have the variable explorer, show help, plots or act as a file explorer:
If the above image is too small on your browser, right click on it and open in a new tab to see it full-size.
To run a program that you’re editing or writing in Spyder, click the
run icon ().
The first time you run a file this may bring up a dialog with some
options: just click “Run” to accept the defaults.
You can see the current working directory for the python shell in the right side of the top bar. To change the working directory, click the “folder button” beside it. Don’t get confused by the path shown in the left side of the top bar - that is the location of the currently open file.
Part B - Excercises & problems#
Exercise 1: Using your IDE and simple debugging#
Download and save this small
python program in your “lab1
” folder:
print_brick_wall_1.py.
Open the file print_brick_wall_1.py
that you saved earlier in your IDE
and attempt to run it. The program has several syntax errors, so it
will not run. (Depending on which IDE you are using, you may even
see some errors highlighted before you try to run the program.)
Identify and correct the syntax errors so that the program runs. The aim of this simple program is to print a “brick wall” pattern on the screen, like this:
--|-----|---
| |
-----|-----+
| |
--|-----|---
| |
-----|-----+
| |
--|-----|---
| |
-----|-----+
| |
If you cannot find or fix the errors, ask your tutor or another student for help. In case you get stuck, a “cheat sheet” is provided here.
However, even after the syntax errors have been corrected, the program may not print what it should (i.e., exactly the output that is shown above). Modify the program so that you get the correct output. Do not write a whole new program! You should understand how this program works, and fix it so that it does the right thing.
Exercise 2: Programming the robot simulator#
Download a copy of the robot.py module and save it in
your lab1
directory.
Before you can do any robot programming, you have to make sure that you can import this module. Remember that python will look for the module in the current working directory (“cwd”). In Spyder, you can change the working directory in the top-right corner of the IDE. (see the introduction to spyder from earlier if you forgot or don’t know how to do this.)
When you run a python program, the cwd is the directory that the
program is in. Thus, an easy way to make import
work is to create
a new program, call it first_test.py
, and save that too
in the lab1
directory.
Note: All your python programs must be saved with a name that ends
in .py
”.
Your program should contain the single statement
import robot
Run the program. After this, you should be able to run robot simulator commands in the shell. Remember that you must start by initialising the simulation:
In [1]: robot.init()
After this, you can test driving the robot around:
In [2]: robot.drive_right()
In [3]: robot.lift_up()
In [4]: robot.gripper_to_open()
In [5]: robot.lift_down()
If you want to see the real robot in action, here is a short video demonstrating it.
Note: The robot simulator may sometimes appear to be frozen (with the window showing “Not responding” or similar). The simulator is not actually frozen in these cases and will continue to respond to commands you execute in the shell. Do not attempt to close it, otherwise it may become actually frozen. When you actually want to close it, if it does not close, simply kill the kernel by pressing the little cross of your shell (see labelled Spyder interface from before).
If you don’t manage to import and initialise the robot simulator, make sure you consult your tutor about it. You will need to be able to do so to be able to attempt homework 1.
The python help system#
Python has a built-in help
function, which gives you access to
documentation in the python shell. Try it out on the robot module:
In [6]: help(robot)
...
Programming problems#
-
The default simulator setup (what you get when you start the simulator with just
robot.init()
) has three boxes on the shelf. It also has a limit of one on the height that the robot can reach. (Height zero is on the table; height one is one step above.)Write procedures (functions) to make each pair of the boxes swap places, i.e., one to swap the left and middle box, one to swap the middle and right box, and one to swap the left and right boxes (ending with the middle box in the middle). Recall that a function definition is done like this:
def swap_left_and_middle(): ...function suite..._
The function suite is a sequence of statements. The extent of the suite is defined by indendation (whitespace before the statement): all statements in the suite must have the same amount of indentation. The standard is 4 spaces or 1 tab.
Remember to identify the assumptions of your function. Do you assume that the robot is standing in front of the left box or the middle one at the beginning? What is the assumed state of the lift and gripper? Document your assumptions in code comments.
You will likely find that defining functions in the shell is quite frustrating. Put your function definitions in first_test.py which you created earlier. Then, after you have writen each function definition, run
first_test.py
, the same way you ranprint_brick_wall_1.py
. You should now be able to call the new function from the shell. -
Can you identify some manoeuvres that are common to all three problems? Split those off into separate functions. Your code should become more compact and easier to read.
-
The simulator allows the robot to lift a stack of boxes, not just a single box. However, if we try to do this with the real robot, chances are the stack will fall over. Try to write your functions so that the robot accomplishes each swap without ever lifting more than one box at a time.
Exercise 3 (finding and using modules)#
Python comes with over 200 modules in the standard library, and many more optional modules can be installed. If you type
In [1]: help()
at the prompt, you enter python’s built-in help system. The prompt will change to
help>
Type quit
(and press Enter) to leave the help system and return to the
normal python shell.
You can get information about a module by typing in the module’s name, for example:
help> math
This will print the complete listing of all functions, variables, classes, etc, that the module defines. This may be a bit more that what you’re ready for at this point, so let’s look at some other ways to find information.
First, most python shells support tab completion. This means that if you type in the beginning of name and press the Tab key, the shell will display a list of defined names that match that beginning. (IDLE will show the list as a popup menu.) This works with modules too, but only if they have been imported. For example, if you type
In [1]: import math
In [2]: math.
and then press the Tab key, you should get a list of all names defined
in the math
module. From the python shell, you can also use the
help function to get information about specific names, such as,
In [3]: help(math.expm1)
Note that here you did not enter into help mode.
Another way to find information is on the web. Open a browser and go to python docs. (By default, the site will show you the documentation for the latest version of python3. If you are looking for documentation for another version, select it from the side bar, or using the menu in the top left corner.) Follow the link “Library Reference”: this will take you to the main index for the python standard library. Find the module you’re interested in on the page, and open its documentation page.
You can also find a lot of useful help just by typing a query into a search engine (such as any one of the 40 active search engines listed on this wikipedia page). Make sure you include the name of the programming language - python. You may also find it that you get more useful answers if you include the version number - 3.X - and specific details about your query.
Problem#
Find a module that has a function that gives you the current date. Use it to print a message, such as:
In [1]: print("Today's date is:", X)
(replacing X with a call to the function that you found).
The print
function prints text to the terminal window. You
can give it several arguments, which will all be printed in sequence,
on a single line. For example,
In [2]: print("The sum of ", 2, "and", 3, "is", 2 + 3)
Note that bits of text are always enclosed in quotation marks.