Download lab.
lab01.py
Lab 0 walked you through how to use the school computers for CS 61A. This lab explains how to use your own computer to complete assignments!, you will periodically login to your class account to check your grades.
Let's login in now. Open up your terminal and type in the following command:
ssh cs61a-??@cory.eecs.berkeley.edu
where
?? is replaced with your two (or three) letter login.
If you're interested, here's an explanation of what the command does:
-
sshis a secure shell (i.e. terminal) that connects to remote servers
-
cs61a-??is the username on the remote server
-
cory.eecs.berkeley.eduis the name of the remote server. Again, here is a list of servers that belong to Berkeley's CS department
You can also watch this video for help.
Once you press enter, the following message will appear:
The authenticity of host 'cory.eecs.berkeley.edu' can't be established. RSA key fingerprint is ... Are you sure you want to continue connecting (yes/no)?
Type in
yes and press enter. Next, you will be prompted for your
password. If you haven't changed your password yet, use the temporary
password on your class account form.
When you type your password, nothing will show up! This is a security feature, not a bug. Continue typing and press enter to login.
Once you are logged in, you'll see something like this:
The first time you login to your class account, your terminal will ask you some registration questions about the following:
The temporary password is not the easiest thing to remember. While still logged in, you should change your password by typing in
ssh update
You will be prompted for the temporary password again. After that, following the instructions for changing your password.
Once you've registered your account and changed your password, you can
log out by pressing
Ctrl-D.".
Windows users can refer to this video for additional help on setting up Python (up to 1:09 into the video).
You will also need to configure your
PATHenvironment variable; the same video describes how to do this from 5:00 to 5:54.
First, open up a terminal, if you haven't already. Since we're learning how to work on your personal computer, make sure you are not logged in to your class account.).
Throughout this lab, we will be using terminal commands (like in lab
0) to manage our files. The first command we'll use is
ls (the
letter
l and the letter
s)..
Our next command is called
mkdir, which makes new
directories. Let's make a directory called
cs61a to store all of
the assignments for this class:
mkdir cs61a
A folder called
cs61a will appear in our home directory. ~.
At this point, let's create some more directories. Make sure you are in
the
~/cs61a directory, using the necessary
cd commands. Then create
projects,
lab, and
lab01 folders inside of our
lab folder:
cd ~/cs61a mkdir projects mkdir lab mkdir lab/lab01
Now if we list the contents of the directory (using
ls), we'll see two
folders,
projects and
lab.
Next, download the template file for this lab, lab01.py. Once
you've done that, let's find our downloaded file. On most computers,
lab01.py is probably located in a directory called
Downloads in
your home directory. Let's use the
ls command to check:
ls ~/Downloads
If you don't see
lab01.py, ask a TA or lab assistant for help.
Let's move our starter file into our new lab directory. From the terminal use the following command:
mv ~/Downloads/lab01.py ~/cs61a/lab/lab01
The
mv command will move the file located at
~/Downloads/lab01.py to the directory
~/cs61a/lab/lab01.
Now, go back into the
lab01 folder that we made earlier.
cd ~/cs61a/lab/lab01
We're ready to start editing a file. Don't worry if this seems complicated — it will get much easier over time. Just keep practicing! You can also take a look at Lab 0 for a more detailed explanation of terminal commands.. You can choose your own editor, but for the purpose of this class, your text editor must
In addition, almost all useful editors have
Here are the text editors that we recommend. We've written short guides for each one to get you started:
Each guide has a section on installing the text editor to your personal computer. For the sake of this lab, you can just jump to there, but come back to finish up the lab!
Note: Please, please, please do not use Microsoft Word to edit programs. Word is designed to edit natural languages like English — trouble will ensue if you try to write Python with Word!
Note: Python usually comes with a text editor called IDLE. We do not recommend it because you won't gain as much experience using the command line to run your Python programs. This is very important once you start working on the projects for this class so we highly recommend that you use the command line to run your files from day 1!
lab01.py
Notice the various blocks of yellow text that are wrapped within three quotation
marks
""". That text is called a docstring, which is a description of what
the function is supposed to do.
Within the docstring, there are lines that begin with
>>>. These lines are
called doctests. Doctests are a great way to explain what the function does
by showing actual Python code: "if we input this Python code (the lines that say
>>>), what should the expected output be (the lines underneath the
>>>)?"
In particular, let's look at the
my_email() function:
The doctest checks that you changed the return value (
return ...) from the
default
'oski@berkeley.edu'.
Note: You should never change the doctests in your assignments! The only part of your assignments that you'll need to edit is the code.
How do we use these tests? Glad you asked!
To run our doctests, switch over to our terminal and type in the following command:
python3 -m doctest lab01!
This will print out a lot of output that shows which tests we are currently failing. You should see something like this:
Oh man, we had 2 failures! :( Before we begin fixing our bugs, let's see what
this output says. In particular, look at the
my_email test that we failed:
Notice it says we had an error in
lab01.py on line 4, in the function
my_email. Now we know exactly where to look for the bug! (This is why line
numbers are a must for text editors.)
After we find line 4, let's try to understand what the test is saying: the
function
my_email, when called with zero inputs, should not return the
string
'oski@berkeley.edu'. The problem is, ours is returning that string!
Change that to your berkeley.edu email:
def my_email(): """Return your last name as a string. >>> my_email() != 'oski@berkeley.edu' True """ return 'jane.doe@berkeley.edu'
Once you've changed the return value of the function
my_email (make sure that
you're returning a string, which has quotes around it), try running the doctests
again from your terminal:
python3 -m doctest lab01.py
The test for
my_email should pass. One down, one to go!
For the second function,
twenty_fifteen, replace the blank line with an
expression that evaluates to the number 2015, using only integers and the
functions
add and
mul. There are many simple ways to do this — try to come
up with the most creative solution! Make sure your doctests pass once you're
done.
You might run your doctests and see that there is no output. This is actually good — it means all the tests passed. By default, doctests only print output when you have failures in your tests.
However, if you want to double-check that you are passing all of the tests, you can add a command line flag
-vto see all the output, including the tests that passed:
python3 -m doctest -v lab01.py
This is called "verbose" mode.
Once all of your tests pass, the verbose output of the doctests (
python3 -m
doctest -v lab01.py) should look something like this:
For more help throughout the semester on fixing bugs, take a look at Albert's debugging guide.