As usual, create two directories for today's class. Create a
directory called lecture18
under activities, and
a directory called lab18
under labs.
Most of you were trying to use the logical operators before. However, I forced you to not use them last week. The reason is simply because last week, you should have been able to write everything without the logical operators. Today, we will explore how the logical operators work, and how we can leverage them to our benefit.
Create a file (using Emacs) called truth_tables.txt
in
your lecture18 directory. In this file, write the truth tables for
the following expressions:
If you put your file in the "wrong" location:
cd ~/cs120/activities/lecture18 cp ../lecture16/truth_tables.txt .
Now that you know the logical operators, you can vastly simplify your
code for date validation. First, copy date_validation.py
from your lab16 directory:
cd ~/cs120/activities/lecture18 cp ~/cs120/labs/lab16/date_validation.py .
You should have three functions: is_leap_year
,
days_in_a_month
, and validate_date
.
Simplify the conditional expressions in each function using your
logical operators. Don't forget to check the month in
days_in_a_month
, to make sure it is in the specified
range.
Challenge: You can completely eliminate conditionals from
is_leap_year
. You can also reduce days_in_a_month to 5
total conditionals.
Back in the dark ages, before smart phones existed, whenever you went to write a text message to your friend you had to type in a letter by hitting the corresponding number on the number pad. Each number (Except for 1 and 0) represented 3 different letters:
Notice that there are no numbers to represent Q or Z. Many phones added upgraded software which allowed Q and Z to be typed, but older phones couldn't input messages with Q's and Z's. So much for texting your friends the word "Quetzalcoatl."
Create a file called old_school_texting.py
in your
lab18 directory. In this file, write a function
t9_word
, which takes a single letter as a parameter.
Using your logical statements, your function should return the numeric
digit that one could press to enter that letter. Your function should
return the string "Error" if the user types Q or Z. You should
only have one return statement in this function. Look at lecture
17's test_me.py
file, to see how you could accomplish
this.
At the bottom of your file, provide test cases for your
t9_word
function. You should provide (in comments!) and
explanation on why you chose your test cases, and an argument as to
why they are sufficient.
Challenge: You can actually accomplish this by using less than 9 conditionals. Do it!
When you have finished, create a tar file of your lab18
directory. To create a tar file, execute the following commands:
cd ~/cs120/labs tar czvf lab18.tgz lab18/
To submit your activity, go to cseval.roanoke.edu. You should
see an available assignment called Lab Assignment 18
. Only
one of your pair should submit your activity. Make sure both partners
are listed in the header of your file.
Do not forget to email your partner today's files!