cs65_lab2
cs65_lab2
cs65_lab2
Now, save the file using the format as follows firstname lastname lab2 task1.py (all in lowercase letters). For
example, I saved my file as md reza lab2 task1.py. You should follow the same file naming format for the
rest of the semester. You will be prompted to save the file in a specific directory. For example, I saved it in
a new folder called ‘cs65’ in the ‘Desktop.’ Feel free to save it in other places of your choice.
Driver and navigator: Now, you will be doing collaborative work for this lab. These exercises should
be completed with your assigned partner. Both of you will take turns playing the role of i) the driver and
ii) the navigator. The driver’s responsibility is to type into Thonny when completing each exercise. The
navigator’s responsibility is to support the driver by reading the exercises, looking up relevant readings, and
identifying errors.
Page 1 of 4
CS65 Lab 2: Playing with functions and variables Due: Thurs, Feb 10, 2022
Tasks and file submission: This lab consists of several small tasks. We expect that you submit separate
file for each task (a python file with a .py extension). In your python file, add comments to briefly explain
what you are doing. For example you could just write:
You should submit your individual copy by acknowledging your partner’s name at the beginning of your
python file (inside a comment); if you could manage to finish it during class, that great! If you cannot
complete the lab during class today, you can complete it individually (or together) sometime before next
Tuesday.
Functions
Task 1: writing simple arithmetic operations with functions
In this task, you will be implementing a python program that calculates simple arithmetic operations such
as add, subtract, multiply, and divide between two numbers. Instead of writing a line of expression in your
python file, you should create a separate function for each of the above-mentioned operations. For example,
here are the empty function headers for the four arithmetic operations. Your goal is to finish the function
body with Python expressions as necessary. Recall that a fragment of Python code, which calculates a new
value, is called an expression.
Once you define a function, you need to call them in order to see its execution. For example, if the definition
is complete for the function add numbers(...), I could the call the function with the following python lines.
Hit the Run button to execute your program and see the outputs.
Define the four functions and call them as instructed. You should call the functions multiple times with
different values.
Page 2 of 4
CS65 Lab 2: Playing with functions and variables Due: Thurs, Feb 10, 2022
• Step 1: First, define the function with a meaningful name and necessary parameters.
• Step 3: return the results (you may need to return two variables).
Now, finish the above steps for remaining three shapes for square, triangle, and circle. First, determine how
many parameters you will need for each shape. For example, for circle you will need only one parameter,
which is the radius of the circle. Then finish the function definition and calling.
Define the four functions and call them as instructed. You should call your functions multiple times with
different values.
Page 3 of 4
CS65 Lab 2: Playing with functions and variables Due: Thurs, Feb 10, 2022
This task is more open-ended! Unlike the previous two tasks, you are not given any initial empty function
header or python code for calling it. The goal is to write Python code from scratch. You should get the
2D coordinates of the start location from the user using built-in input() function. Similarly, also get the 2D
coordinates of the end location from the user. Besides showing the distance with fractions, you also need to
show the following outputs. By using various mathematical operations such as floor(), ceiling(), round()
you either need to chop off the fractional part of the distance or add in an additional fraction to make it an
integer number. You have used several other built-in functions by now, such as print(), input(), int(), etc..
There are several other Python built-in functions in math library. You may find it useful to use them.
Define the necessary functions and call them. You should call the functions multiple times with different
values.
Page 4 of 4