Lab: Using Conditionals
Preparation
Boot up your computer into Mac OSX, open up a Terminal
, and open Brackets
. You may save your work anywhere on your computer, but it is a good habit to store your code on the COURSES
drive in your StuWork
directory. After mounting it by double clicking the icon on your desktop, you can change directories to it in the Terminal
with:
$ cd /Volumes/COURSES/cs111-03-s19/StuWork/USERNAME/
$ mkdir lab-04-10
$ cd lab-04-10
Exercise 1
In the last lab, you were asked to examine the following expression where val
, lower
, and upper
are previously defined integers.
val = min(max(val,lower), upper)
a. Discuss with your partner and make sure you both understand what the expression is doing and what val
will become. (You might consider thinking about what happens when lower = 0
and upper = 100
.)
b. Examine the code below and discuss with your partner whether you think the value of val
will be the same after the code is evaluated or different. Why?
if val < lower:
val = lower
elif val > upper:
val = upper
else:
val = val
c. Which of the two ways do you prefer? Discuss with your partner.
Exercise 2
In this exercise, we will practice reading code and determining how the if statements will be evaluated. Look through the following piece of code and then answer the following questions.
val = int(input("Enter an integer: "))
if val == 5:
print("red")
elif val < 5:
if val >= 0:
print("green")
else:
print("blue")
else:
if val > 10:
print("purple")
(Since each of the following parts ask you to run the code, it might be useful to save the above program in a rgb_example.py
file and then run it with python3 rgb_example.py
each time.)
a. Under what values of val
will red be printed? Verify your answer by executing your code on that value of val
.
b. Under what values of val
will green be printed? Verify your answer.
c. Under what values of val
will blue be printed? Verify your answer.
d. Under what values of val
will purple be printed? Verify your answer.
e. Under what values of val
will nothing be printed? Verify your answer.
f. Examine the following conditional and determine if it is equivalent to the conditional structure above.
if val < 0:
print("blue")
elif val < 5:
print("green")
elif val == 5:
print("red")
elif val > 10:
print("purple")
g. Do you think the first or the second conditional is easier to read? Why? Discuss with your partner.
Exercise 3
a. Examine the following two pieces of code to make sure you understand that they are equivalent. Discuss with your partner why this is the case.
if condition1:
if condition2:
do_something()
if condition1 and condition2:
do_something()
b. Examine the following two pieces of code to make sure you understand that these are not equivalent. Discuss with your partner why this is the case.
if condition1:
do_something()
if condition2:
do_something()
if condition1 or condition2:
do_something()
Exercise 4
Here is a common interview question in CS.
a. Write a program called fizzbuzz.py
that asks the user to input a number num
and outputs the following.
- Outputs “fizz” if
num
is divisible by 3, - Outputs “buzz” if
num
is divisible by 5, - Outputs “fizzbuzz” if
num
is divisible by both 3 and 5, and - Outputs
num
otherwise.
b. Modify your program so that it also outputs “jazz” if num
is divisible by 7. Note that it should also output “fizzjazz” if it is divisible by 3 and 7, and so on for all other combinations above.
c. How would you modify your program to support additional number-string combinations? For example, printing “pezz” if num
is divisible by 9 and printing “tyzz” if it is divisible by 11.