Lecture 16 - Complex Conditionals


As usual, create two directories for today's class. Create a directory called lecture16 under activities, and a directory called lab16 under labs.


In-class Activity 1

Best of 7

Copy your RPS.py program from the last lab, using the cp command:

cd ~/cs120/activities/
cp lecture15/RPS.py lecture16/
cd lecture16

Alter your code so that you play the computer 7 times. You should total how many times the computer won, versus how many times the player won. You should indicate, at the end of the program, which player won the set of games.


Complex Conditionals

Conditionals are much more powerful that were revealed on Monday. One thing we can do is nest the conditionals inside one another, similar to how we can put if statements within for loops. Another of the key pieces of conditionals are they can be used to select a particular block of code to execute. Using the elif clause of an if statement block, we can string together what multiple cases with one if condition.


In-class Activity 2

Grade Conversion

In a file grades.py, create a function convert_to_letter_grade. The function takes a numeric grade from 0 to 100, and prints the associated letter grade using the following scale:

A
[90, 100]
B
[80, 90)
C
[70, 80)
D
[60, 70)
F
[0, 60)

Note that square brackets mean inclusive, and parenthesis means exclusive.

Test your function by reading in a numeric grade from the command line, and check the output of the function. How many test cases should you have for this program? We'll talk more about testing on Friday.


Lab Assignment 16

Date Validation

The reason we have leap years added to our calendar is because they do not take into account the fact that the Earth actually takes (about) 365.25 days to revolve once around the sun. While it sounds like this should be an easy thing to account for, there's actually some fairly complex math involved in dealing with this inconsistency. This causes havoc when trying to validate a date, but we can simplify things by adding functions and using our conditionals wisely.

In a file called date_validation.py, write a function called is_leap_year. This function should take a single parameter, an integer representing some year. Your function should return True if and only if the parameter is a leap year.

A leap year occurs almost every 4 years. All leap years are divisible by 4, but not all years that are divisible by 4 are leap years. Years that are divisible by 100, but not divisible by 400 are not leap years.

Before you go any further, test your function using years that are, and are not leap years. Does it compute the correct value?

We can use the above function to aid in a computation of the number of days in a month. Create a function days_in_a_month in the same file. This function should take two parameters, an integer in the range [1, 12] that specifies a month, and another integer specifying a year. Your function should return the correct number of days in the specified month, taking into consideration leap years.

Recall:
30 days hath September, April, June, and November
All the rest have 31
Except for February, the only one.

Again, before you go any further, test THIS function. Try it with multiple months in multiple years to make sure it functions correctly.

Finally, create a function called validate_date. This function should take 3 integer parameters, representing: a day, a month, and a year. This function should use your previously written function, and should return True if and only if the date specified is truly a date. Think about what would constitute an invalid date. Make sure your code handles all extraneous cases.

Test your code on various dates. Think about what a valid number of test cases would be for this program. We will talk more about testing on Friday.


Submission

When you have finished, create a tar file of your lab16 directory. To create a tar file, execute the following commands:

cd ~/cs120/labs
tar czf lab16.tgz lab16/

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 16. 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 todays files!


Last modified: Wed Oct 2 10:30:56 EDT 2013