Albion College

CS 171

Introduction to Computer Science I & Lab

Fall 2021

Lab 2: Conditionals and Booleans

The purpose of this lab is to get you used to using Python, to start experimenting with Python conditionals and boolean expressions. Do not rush through this lab. Whatever you do not complete in lab today, you should try and complete on your own or with your lab partner in the next few days.

Rules For The Lab

You will play two different roles in the lab. You will either be the driver or navigator.

You have been assigned identifiers s1 and s2 when you entered the lab. At the start of the lab, s1 will be the driver and s2 will be the navigator. Your roles will switch during the lab.

Whatever you do not complete in lab today, you should try and complete on your own or with your lab partner in the next few days.

Warmup

Your first task is to introduce yourself to your lab partner. Tell your lab partner your name and how your semester is going so far.

Background

In Python, variables of type Boolean can represent a Boolean value that is either True and False. It is important to note that the Boolean variables True and False are different from the strings 'True' and 'False'. Boolean variables

We have seen several ways to conditionally perform computations that rely on boolean logic:

The result of a comparison (such as <, >, <=, >=, ==, !=) is also a Boolean. Variables of type Boolean can be combined with Booleans to yield new Booleans based on standard Boolean logic, defined as follows

XYX and YX or Ynot X
TTTTF
TFFTF
FTFTT
FFFFT

Using boolean variables, one can create complex expressions that represent many situations.

For example, one might want to say \[x < y < z\] but this is equivalent to \[x < y \quad \text{AND} \quad y < z.\] Python evaluates the relational operators from left to right. Thus, x < y < z will likely give the wrong answer because it is evaluated as (x < y) < z, where (x < y) is either True or False; Python then evaluates either True < z or True < z by converting the Boolean values to their integer equivalents (True == 1 and False == 0). Instead of x < y < z you will need to express this using Boolean operators as (x < y) and (y < z) for correct results.

Tasks

Download the lab

Download the source file Lab2.py and save it on your computer. It contains the stubs of several Python functions. Save it directly to your folder with your other lab files and open it in the Python environment. Complete the stub methods to perform the indicated function. Verify your code by running the program. Add additional test cases as needed.

Follow the following procedure through the lab.

  1. Read the question/problem. If the question asks you to guess what will happen before you type the code, then work with your lab partner to guess what will happen.
  2. Enter the code, exectute it, and see what happens.
  3. Discuss the results with your lab partner. See if you can come up with a reason why you got the results given. If the results are different than what you expected, try to figure out why that is the case.
  4. Click on the explanation link for the problem (if it exists) and read an explanation for the actual behavior of the code fragment.
  5. Ask you your instructor if you still need clarification.

In most cases, your function will not print any values. Rather, your function will return values to the calling function and any values will be used there, including possibly printing.

# Lab 2 # YOUR NAMES HERE # returns the absolute value of x: # x, if x is positive or 0 # -x, if x is negative # don't use the predefined math.abs() function! def ABS(x): # your code here return 0 # returns True when n is even # returns False when n is odd def isEven(n): # your code here return -1 # returns True when n is odd # returns False when n is even def isOdd(n): # your code here return -1 # return the maximum of a, b, c def max(a, b, c): # your code here return -1 # return the minimum of a, b, c def min(a, b, c): # your code here return -1 # returns True if (x,y) is inside the rectangle with corners (x1,y1) and (x2,y2) # Assume x1 < x2 and y1 < y2 # Hint: use the and operator def inRectangle(x, y, x1, y1, x2, y2): # your code here return -1 # returns true if the year is a leap year: # when a year is divisible by 4 (true), unless # the year is divisible by 100 (false), unless # the year is divisible by 400 (true) # Leap years: 2000, 2016, 2020, etc # Non-leap years: 2017, 2018, 1900, 2100, etc def isLeapYear(year): return -1 # returns the number of days in a given numerical month (1-12) and year # 1 (January) has 31 days # 2 (February) >>>> 28 (normally) or 29 (leap year) # 3 (March) >>>> 31 # 4 (April) >>>> 30 # 5 (May) >>>> 31 # 6 (June) >>>> 30 # 7 (July) >>>> 31 # 8 (August) >>>> 31 # 9 (September) >>>> 30 # 10 (October) >>>> 31 # 11 (November) >>>> 30 # 12 (December) >>>> 31 # Hint: Use the or operator and your isLeapYear() function def daysInMonth(month, year): return 0 # compute the numeric day of the week, h, for a given date # Use Zeller's algorithm to compute h: # h = (q + (26(m+1))/10 + K + K/4 + J/4 + 5J) mod 7 # where all division is done as integer division # q is the day of the month; # m is the month (3 = March, 4 = April, 5 = May, ...). # January and February are considered the 13th and 14th months # of the previous year; # K the year of the century (0, 1, ..., 99); # J is the leading two digits of the year. # h corresponds to the day, where # Saturday = 0, Sunday = 1, ..., and Friday = 6. # For example, to compute the day of the week for January 6, 2021, # q = 6, m = 13, K = 21, and J = 20; this will result in h = 4 (Wednesday). def zeller(q, m, K, J): # your code here return -1 # compute the numeric day of the week, h, for a given date # compute q, m, K, and J from dd, mm, and yy # use the day function above def day(dd, mm, yy): # your code here return -1 # converts an integer code into a string representing the day of the week # 0 is Saturday # 1 is Sunday # 2 is Monday # 3 is Tuesday # 4 is Wednesday # 5 is Thursday # 6 is Friday def dayName(day): # your code here return -1 # converts an point percentage into a grade base on our syllabus # http://zeta.albion.edu/~dreimann/Fall2021/courses/cs171/ def grade(percentage): # your code here return -1 # main function for testing def main(): print("Lab 2") print("ABS:") print(ABS(-10), "should be 10") print(ABS(10), "should be 10") print(ABS(0), "should be 0") print("isEven:") print(isEven(-2), "should be True") print(isEven(-1), "should be False") print(isEven(0), "should be True") print(isEven(1), "should be False") print(isEven(2), "should be True") print("isOdd:") print(isEven(-2), "should be False") print(isOdd(-1), "should be True") print(isOdd(0), "should be False") print(isOdd(1), "should be True") print(isOdd(2), "should be False") print("Maximum:") print(max(1,2,3), "should be 3") print(max(1,3,2), "should be 3") print(max(2,1,3), "should be 3") print(max(2,3,1), "should be 3") print(max(3,1,2), "should be 3") print(max(3,2,1), "should be 3") print("Minimum:") print(min(1,2,3), "should be 1") print(min(1,3,2), "should be 1") print(min(2,1,3), "should be 1") print(min(2,3,1), "should be 1") print(min(3,1,2), "should be 1") print(min(3,2,1), "should be 1") print("inRectangle:") print(inRectangle(0,0, 10,20, 15,40), "should be False") print(inRectangle(15,0, 10,20, 15,40), "should be False") print(inRectangle(25,0, 10,20, 15,40), "should be False") print(inRectangle(0,30, 10,20, 15,40), "should be False") print(inRectangle(15,30, 10,20, 15,40), "should be True") print(inRectangle(25,30, 10,20, 15,40), "should be False") print(inRectangle(0,50, 10,20, 15,40), "should be False") print(inRectangle(15,50, 10,20, 15,40), "should be False") print(inRectangle(25,50, 10,20, 15,40), "should be False") print("isLeapYear") print(isLeapYear(2000), "should be True") print(isLeapYear(1900), "should be False") print(isLeapYear(2020), "should be True") print(isLeapYear(2021), "should be False") print("daysInMonth") print(daysInMonth(2, 2000), "should be 29") print(daysInMonth(2, 1900), "should be 28") print(daysInMonth(9, 2021), "should be 30") print(daysInMonth(10, 2021), "should be 31") print("Zeller") print(zeller(7, 9, 21, 20), "should be 3") # 9/7/2021 print(zeller(1, 13, 21, 20), "should be 0") # 1/1/2022 print("Day") print(day(7, 9, 2021), "should be 3") # 9/7/2021 print(day(1, 1, 2022), "should be 0") # 1/1/2022 print("Day Names:") print(dayName(-1), "should be ERROR") print(dayName(0), "should be Saturday") print(dayName(1), "should be Sunday") print(dayName(2), "should be Monday") print(dayName(3), "should be Tuesday") print(dayName(4), "should be Wednesday") print(dayName(5), "should be Thursday") print(dayName(6), "should be Friday") print(dayName(day(7, 9, 2021)), "should be Tuesday") # 9/7/2021 print(dayName(day(1, 1, 2022)), "should be Saturday") # 1/1/2022 print("grade") print(grade(93.5), "should be a 4.0") print(grade(73.5), "should be a 2.0") print(grade(37.9), "should be a 0.0") # call the main function main()

Final Steps

Save you code!

Send your instructor and lab partner the .py file as an email attachment (one per group). Also make sure each lab partner has a copy. You are now done with the lab!