Assignment 0 Tutorial

This tutorial will help you complete a fictitious coding assignment. Along the way, it will show you how to use all of your fancy new coding tools.

Prerequisites

At this point, you should have:

If you are missing any of these, go back and complete the previous setup steps before proceeding.

Starting an Assignment

To start out, walk through the steps you’ll follow when we give you a new assignment.

First, click on this “invitation link”: https://classroom.github.com/a/lGoDREYF. You’ll get a new invitation link for each assignment. (If necessary, sign in to GitHub using the username and password you created earlier.)

Second, you’ll see a screen asking you to “accept” the assignment. Click the button to do so.

Third, you’ll see a screen saying you have accepted the assignment and are ready to go. The important part is the link at the bottom which says your assignment has been “created here”. Click on the link.

Assignment accepted

Fourth, you will see a page with the name of the assignment at the top. This is your own private copy of the assignment on GitHub. This is a Git repository, but it lives in a folder on GitHub’s computers, rather than on your computer. In Git lingo, this is a “remote” repository (i.e. somewhere else) rather than a “local” repository (i.e. on your computer). This repository is visible only to you and the course staff. You will “pull” the assignment from this repository on GitHub to your own computer, make changes on your computer, and then “push” those changes back to this repository on GitHub. This is exactly how software developers use GitHub to collaborate on projects: they pull from a shared repository, make changes on their computers, and then push their changes back to the repository.

Assignment 0 GitHub Repository

Fifth, copy the URL from the browser window pointing to the assignment repository on GitHub.

Copy this URL

Sixth, open GitHub Desktop. Select “Clone Repository” from the File menu. Paste the GitHub repository URL into the upper text field. Then click on “Choose…” next to the lower text field and select the FMS directory you created to hold all of your work for the course. This will “clone” your private assignment repository on GitHub into a local copy on your computer.

At this point, GitHub Desktop should look like the image below. Since you haven’t yet made any changes, the main pane will be blank.

GitHub Desktop Cloned

You can leave GitHub Desktop alone for a bit. We’ll come back to it later.

Congratulations! You’re ready to start work.

Running Code

In GitHub Desktop, click on the link labeled “Open this repository in Finder” (on macOS) or “Open this repository in Explorer” (on Windows). This will open the usual window for browsing folders on your computer. You should see a folder with a name like fms-assignment-0-username (with your GitHub username instead of “username”). This folder contains all of the files we’ve given you for this “assignment.” You downloaded them to your computer in step six, above, when you copied your GitHub repository to a local repository on your computer.

Have a look inside. You should see three files:

Open up Atom, select Open from the File menu, and find and choose the README.md file in the fms-assignment-0-username folder in your FMS folder. Read the directions.

Now that you know that your goal is to print out the chorus of Empire State of Mind, let’s see how much of the chorus the program currently prints. To do this, you will need to run newYork.py.

Open a command-line window in the fms-assignment-0-username folder according to the instructions in the Installing Anaconda tutorial. Then activate the FMS programming environment by typing

$ source activate FMS

Then run newYork.py by typing

$ python newYork.py

This tells your computer to run newYork.py as a Python program. It should print out:

There's nothing you can't do

That’s a good start, but surely there’s more to the chorus than that. Go back to Atom and use File > Open to open lyrics.txt. (Or just click on lyrics.txt in the left sidebar.) So your goal for the “assignment” will be to modify newYork.py to print out the next four lines as well.

Editing Code

So far, you’ve just been using Atom to look at the contents of files. Now it’s time to use it to write code as well. Open newYork.py in Atom. You should see:

newYork.py (unmodified)

The numbers on the left are line numbers; they’re a convenience for helping you find things in your program but they’re not part of the program itself. More interesting are the two lines of text.

The first line is a print command, like the one we used in our Hello World example in class. As a reminder, the way that print works is that the text to be printed is enclosed within parentheses and then within single quotation marks. Atom helpfully colors that text green so that you can see it’s what will be printed.

What about the two backslashes in the line? Those are there because the lyrics contain apostrophes, i.e. single quotation marks, so Python might get confused and think those are actually the end of the text it’s supposed to print. Each backslash tells Python that “The next quotation mark isn’t the end of the text to print, it’s just an actual quotation mark which you should print as a quotation mark.

The second line is a comment. In Python, anything after a # is ignored. Programmers can use this to leave notes for themselves, or for others. We’ve used this to leave you a short note telling you where to make your changes.

It’s time to take stock. This program has one print command, and it prints one line of the lyrics from Empire State of Mind. Your goal is to print five lines of the chorus. So you probably need … five print commands.

Select the second line of the program (the one with the comment). Delete it. Now type a print command with the second line of the lyrics:

print('Now you're in New York')

You should see:

newYork.py (first change)

Wait! That doesn’t look right. Why is your new print command all different colors? It looks like the green color of the text to be printed stops in the middle of you're. Something is wrong with this modified program. Sure enough, if you save your work (File > Save) and then go back to the command line to run the program, you’ll get an error message:

$ python newYork.py 
    File "newYork.py", line 2
  print('Now you're in New York')
                ^
SyntaxError: invalid syntax

Go back to Atom. Let’s see if we can find and fix the problem. It looks like everything is okay up until the apostrophe in you're. Remember what we said above about “escaping” apostrophes in quoted text? Python thinks the text to be printed ends in the middle of you're and is confused by everything else on the line.

Fortunately, this has an easy fix. Add a backslash before the apostrophe on line 2. Now you should see:

newYork.py (second change)

That looks better, doesn’t it? Check that this is right by saving the file in Atom, going to the command line, and running the program again:

$ python newYork.py 
There's nothing you can't do
Now you're in New York

Much better! Now you’re getting somewhere.

Committing Your Changes

A successful tweak to your code is a great occasion to use Git to commit your changes. Switch back to GitHub Desktop. The window should look something like this:

GitHub Desktop Cloned

The left pane contains a list of files that have been changed since your last commit. (In this case, that was when you created the Git repository on your computer, i.e., the version of the files we gave you.) That list should contain a single item (“1 changed file”): newYork.py.

The right pane shows you the actual changes in that file. The lines in red have been removed; the lines in green have been added; the lines in grey are unchanged. Lawyers will recognize this as a kind of redline. Programmers would call this a “diff”: it shows the differences between two versions of a file. Right now, it’s showing that you deleted the “Put the rest of the chorus here” comment and added a new line to print the second line of the lyrics.

That looks good. Let’s commit it. Type a short summary of the change in the “Summary” text field at the bottom of the left pane. Something like “Added second line” would be good. Notice that the “Commit to master” button turns bright blue once you do. When you’re satisfied with your summary, click the button.

Commit Example

Well done!

Now the GitHub Desktop window should look like it did when you started: the left pane says “0 changed files” and the right pane is blank. That’s because you haven’t made any changes since your last commit a few seconds ago.

But something IS different now. Click on “History” at the top of the left pane. It now shows a list of all the commits in the entire history of this project. The topmost one the one you did just now: it should show the description you just gave it. (Beneath that, you should see a few commits Professor Grimmelmann did when setting up the assignment.) Click on your most recent commit at the top. The right pane now shows a list of the changed files (in this case just newYork.py) and the diff of the changes to them. Click on some of the other commits and notice that they made changes to different files. This is how Git lets you browse the entire history of work on a project quickly and conveniently.

What if you decide you’ve made a huge mistake and you don’t like your changes? Let’s fine out. Right-click on your most recent commit at the top of the left pane and select “Revert This Commit” from the pop-up menu.

Now switch back to Atom. Look at the code in newYork.py again. It should look different. The file has reverted to the way it was before you committed the change to add the second line. So if you need to back out of a mistake, you can revert the commit when things went wrong, and your code will appear as though it never happened.

It’s a shame to lose your work, though. Fortunately, as we said in the Git tutorial, Git is very good about never forgetting anything you store with it. It’s easy to get back to the improved version of newYork.py. Switch back to Github Desktop. Right click on the topmost commit in the left pane (which should say “Revert (name of previous commit)”. Select “Revert This Commit” from the pop-up menu. Look back in Atom and you should that see your second line is back. Reverting a revert is a double negative; it leaves things exactly the way they were, as though you’d never reverted at all.

Submitting Your Work

Once you made a commit, the GitHub Desktop window changed in a small way. A button appeared on the top right reading “Push origin.”

Commit Example

This button “pushes” your commits back to GitHub. This is the opposite of the “pull” you did when you set up the assignment 0 repository on your computer. Now that you have some changes to share back, clicking this button sends them back to the course staff, When you’re done with an assignment, all you have to do is click this button and we’ll have your work, ready to go.

In fact, this is so easy that we encourage you to click the button early and often. Better to push too often than not enough. Don’t worry about pushing us incomplete or non-working code. We will only grade your code based on the final state of your repository at the time of the deadline.

In this case, you may not have the complete chorus in your program yet, but you still have some meaningful progress. Click the button.

Finishing “Assignment” 0

Two lines down, three to go. Use Atom to add the remaining three lines the same way you added the second. Use the command line to test that your modified program works correctly. Then use GitHub Desktop to commit your changes and push them to GitHub.