These exercises should be completed with your assigned partner. Both of you will take turns playing the role of the driver (the one currently screen sharing) and the navigator. The driver’s responsibility is to type into Thonny, etc., when completing each exercise, and the navigator’s responsibility is to support the driver by reading the exercises, looking up relevant readings, and identifying errors. At all times you should be working together on the exercises rather than completing them independently.
Before you begin, you should ensure that today you perform opposite roles as the previous lab.
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()
Create a new file named
move_circle.py and copy the code from the Move Circle Exercise in a previous lab.
a. Modify the program so that the W, A, S, and D keys move the circle instead of the Up, Left, Down, and Right keys.
b. Modify the program to draw a horizontal line through the center of the window that divides the top and bottom halves of the window. Then add an extra conditions that will change the color of the circle to be blue if the circle is in the bottom half of the window and red if it is in the top half.
c. Modify the program once more so that the window is cut into quadrants and the color switches between four different colors—one for each quadrant it transitions into.
Here is a common interview question in CS.
a. Write a Python script called
fizzbuzz.py that, when executed, asks the user to input a number
num and outputs the following.
- Outputs “fizz” if
numis divisible by 3,
- Outputs “buzz” if
numis divisible by 5,
- Outputs “fizzbuzz” if
numis divisible by both 3 and 5, and
Hint. You can use the modulus operator
% to check if one number is divisible by another. For more information, look at the Floor division and modulus section of today’s reading.
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.
Continue working on Assignment 4 together!