MTEC1003 Media Computation Skills Lab

Fall 2023 Wed 6:00-8:30 [ONLINE] Prof. Louis Goldford.

SYLLABUS SCHEDULE SOFTWARE + RESOURCES GRADING

Lab 9 / Part 2: Python Functions

Note that ALL SOLUTIONS TO THESE PROBLEMS MUST BE SHOWN IN THE FORM OF A FUNCTION DEFINITION AND A CALL TO THAT FUNCTION.

C O N T E N T S

Setup for Python3

  1. Recreating JavaScript Functions Exercises in Python

Setup for Python3

Let’s be sure your Terminal session will run Python3 (instead of the prepackaged Python 2.7.x) when calling python on the command line. We can create an “alias” for python3 by running the following command:

  $ alias python=/usr/local/bin/python3 

Alternatively, you can simply run python3 in the Terminal instead of python, but this gets annoying after awhile.

Once you’ve done this, you’re all set.


1. Recreating JavaScript Functions Exercises in Python

NOTE: This section starts with the same exercises from Lab 9 / Part 1. If you feel more comfortable with Python rather than JavaScript, you can start with this section in Python and then complete the JavaScript exercises last. Either way, click on the link below for Lab 9 / Part 1 to see the details for each program.

In Lab 9 / Part 1, you made 4 programs:

  1. greetings.html
  2. pluralize.html
  3. double.html
  4. factorial.html

Using your knowledge of Python, aided in part by this week’s Python Function Definitions + Calls slides, translate your HTML/JavaScript programs into Python and save them as: greetings.py, pluralize,py, double.py, and factorial.py.

Make sure they’re in your /lab-09-functions repository!

Now we just have to test our python scripts by running them on the command line.

Back in Terminal, simply type python, then a space, and then drag-and-drop one of your new files, e.g. pluralize,py, onto the Terminal window (remember: this will quickly create a full path to your file on the command line!). Run the command by hitting enter, of course.

Make sure your file is included in your /lab-09-functions repo, and don’t forget to add, commit, and push your changes. Use your git cheatsheet (from lab 3) if you need it.


At the end of this lab, your /lab-09-functions folder should contain the following files: