MTEC1003 Media Computation Skills Lab

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

SYLLABUS SCHEDULE SOFTWARE + RESOURCES GRADING

Lab 7 / Part 2: Python Conditionals

C O N T E N T S

Setup for Python3

  1. Recreating JavaScript Conditional Exercises in Python
  2. Reminders (User Input)
  3. Modified Reminders

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 Conditional Exercises in Python

NOTE: This section starts with the same 3 exercises from Lab 7 / 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 7 / Part 1 to see the details for each program.

In Lab 7 / Part 1, you made 3 programs that returned different results depending on user input:

  1. cake.html
  2. spanish.html
  3. oddcalc.html

Using your own knowledge of Python, aided in part by this week’s “Booleans + Conditionals” slides, translate your 3 HTML/JavaScript programs into Python, and save them as: cake.py, spanish,py, and oddcalc.py.

Make sure they’re in your /lab-07-conditionals 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. cake.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-07-conditionals repo, and don’t forget to add, commit, and push your changes! Use your git cheatsheet (from lab 3) if you need it!


2. Reminders (User Input)

Write a Python program that:

Hour Reminder
0 to 5 “It’s early. You should be sleeping!”
5 to 7 “Wake up, brew that coffee, get that mile run in, and get that breakfast…”
7 to 9 “Time to go to work.”
9 to 12 “You should be working!”
12 to 13 “Take your lunch break!”
13 to 17 “Do you feel that afternoon lull?”
17 to 19 “Time to hit the gym…”
19 to 21 “Gotta eat that dinner.”
21 to 23 “Get that SLEEP. And rePEAT!”
any other hour “You didn’t type an acceptable value! (0-23)”

Save your code, calling it reminders.v01.py.

Save your code, make sure your reminders.v01.py file is included in your /lab-07-conditionals folder, and don’t forget to add, commit, and push your changes! Use your git cheatsheet (from lab 3) if you need it!


3. Modified Reminders

Save a new version of your reminders.v01.py file, and call it: reminders.v02.py.

In this new version, let’s modify the current code so that instead of user input, the program takes the actual current time.

import datetime

At the top of your new file on line 1, type the following:

 import datetime 

import is a statement in Python that allows you to “import” a “module.” In this case, datetime is the module we’re importing. A Python module is basically a file containing more Python code; more functions, variables, classes, methods, etc. We can use datetime to easily read the current system time and convert between a variety of formats. We can even use this set of tools to convert time between time zones!

Write the rest of your code below your import datetime line.

You won’t need to write much that’s new. You’ll need to comment out the existing user input line, where you previously used input() and float(). In its place, you’ll need to use the datetime module to grab the current hour. It’s really easy! just type the following:

  now = datetime.datetime.now()
  print(now.hour)

Run your code in the Terminal, and you’ll get a number between 0 and 23. This is your current hour. You can read more about how the datetime module works here, but suffice it to say that we’re calling the class object datetime within a module that also has the same name. So, that’s why our syntax includes funny things like datetime.dateime.

Anyway, by printing now.hour you can easily retrieve the current hour in local time. Use its value with your conditions! Finish adapting your code so that your conditions respond to this current hour. Make sure to save frequently and test on the command line!

Save your code, make sure your reminders.v02.py file is included in your /lab-07-conditionals folder, 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-07-conditionals folder should contain the following files:

Now, on to Lab 7 / Part 3!