e

Assignment 1: Your First Function

http://www.lefthandedtoons.com/245

Initial Setup

Make sure that Visual Studio Code is installed (or, if you have opinions about text editors already, you can use your editor of choice). You can find Visual Studio Code at Microsoft's website. Once it's installed, avail yourself of my video walkthrough for configuring the program for Python development.

The key points are to install Visual Studio Code (henceforth VS Code); install its Python extension; and make sure you have a Python installation on your computer (on Windows, from python.org; on Mac, via Homebrew and a terminal command like brew install python3; or the system installation of Python on Linux).

The File System

Whether you're enrolled in CSCI 051 PO or not, the first lab for that course has a great introduction to the concept of a filesystem and basic terminal commands.

You can navigate to different folders on your computer by opening Finder on Mac OS, File Explorer on Windows, or Nautilus (or Files) on most Linux distributions. Typical icons look something like this:

Opening one of these programs typically will show your home directory, a folder in which you can place all your stuff. Some of the folders inside of your home will be provided by the operating system (e.g., "Pictures", "Music", or "Downloads"), but you can make your own as well. Double-clicking a folder typically enters or opens the folder, showing the files and folders inside; there is also typically a Back button to take you back to the previous folder.

A good step at this point is to make a folder in your home directory called CS50; inside of that folder, you could make other folders (notes, assignments, etc). It's probably a good idea to make a series of folders: CS50 containing assignments containing assignment1, which we can write more succinctly as CS50/assignments/assignment1. You can then open that assignment1 folder in VS Code and you should be ready to go!

In the future, assignments may come with starter code or supplemental files that are necessary to proceed with the task, and your web browser will typically download these into your "Downloads" folder. You will need to move them into your assignment folder, but how? You may already know that you can "drag and drop" to move a file or folder from one location to another. However, sometimes, "drag and drop" is not easy; for example, you may want to move multiple files or you may find using a trackpad challenging. Here, the idea of copy and paste which you may know from text editing can be helpful.

After selecting some files, you can Copy (via the edit menu or the right-click menu, or by simultaneously pressing the keys Command+c on Mac, or Control+c on Windows or Linux); navigate to the destination folder and you can Paste these files (again via the menu or by using Command+v or Control+v). Note that copying and pasting will keep the item at the original location, and create a new copy of the item in the new location you paste to. If you want to just move the item to the new location without keeping it in the original location, you should instead use Cut, whose shortcut is Command+x or Control+x.

As an aside, right-clicking on a Mac is possible on a single-button mouse by holding the Control key while clicking, or by making a two-finger tap or click on a trackpad. On Windows computers there is often a dedicated pop-up menu key.

Your First Function

Your first task: Define a function called sphere_surface_area that takes the radius as a parameter and returns the surface area of a sphere with that radius.

Recall that the basic structure for defining a function is:

    def function_name(parameter1, parameter2, ...):
        statement1
        statement2
        ...
        return something # not all functions will need return statements!
  

Remember that the way that Python can tell what is part of the function is based on the indentation.

One great way to play around with your code is to start your own Python process in VS Code's terminal, by typing python and hitting return; after that, you can write from assignment1 import * and hit return at the Python prompt (which looks like %gt;%gt;>). Then you can call your own functions:

    >>> sphere_surface_area(12.4)
    1932.2035136000002
  

If you modify the code, you should save it (Command+s or Control+s) and import it again. You could also add calls to your function at the end of your Python file:

    ... (all your definitions) ...
    print(sphere_surface_area(12.4))
    print(sphere_surface_area(20))
  

Once you're comfortable with running Python programs with your own defined functions and with playing with them in Code, move on to the main part of this assignment. If you can, it's worth getting used to defining and using a keyboard shortcut to run your program so you don't need to switch over to the mouse so often (VS Code defaults the the F5 key for this purpose).

A Semester Abroad

http://www.bildergeschichten.eu/euro_cartoon_witz.htm

You're going to do a semester abroad in Europe and have decided to write a few functions that will help you out with some common questions you might find yourself asking while you're there.

A few pieces of advice:

Commenting and Documentation

An important part of programming is making sure that your code is well documented so that when someone else looks at it (or, more likely, you look at it a month later) you can get information about the program without having to read all of the code. Any sufficiently tricky line should have a comment explaining its motivation and mechanism, and long functions especially should have comments throughout to guide the reader.

It's also important to use good style in programming, just as in writing. This includes following naming conventions (e.g., variables and functions should be lowercased), using good variable names (miles is much better than x or tmp), and properly using whitespace to support the (human) reader's understanding of the code.

Grading

When you've finished and are satisfied your code works properly, upload your assignment1.py file to Gradescope. Note that you can resubmit multiple times if your code has errors. There are many components of this assignment, so get as far as you can but also feel free to submit early and submit often! It's a good idea to submit after each part of the exercise.

Besides the points for functionality, a penalty for code style issues will be given out by (human!) graders. This penalty will increase in magnitude throughout the course, so do read the line-by-line comments on your Gradescope results.