Lab 2 - Functions (v1.3)
Lab 2 - Functions (v1.3)
Lab 2 - Functions (v1.3)
COR-IS1704 – Lab 2
Functions
Instructions
Download and extract the files from the Lab2_Starting_Code.zip. Save the files into a folder of your
choice – you should obtain several *.py files. Enter your code into the respective *.py file to
complete the exercises.
Q1 [ * ]: Code Tracing
Given the following codes in q1.py, could you predict what is going to be the output of the code
without running the code?
You may want to use a sheet of paper to note down the various values of the variables stored in
memory as well as what is displayed on the screen (if applicable).
Run the code in q1.py provided to check whether your guess is correct.
def perform_magic_1(x):
x = x + 1
def perform_magic_2(x):
x = x + 10
return x
def perform_magic_3(y):
return (y + 3)
def perform_magic_4(z):
z = z + 1
z = perform_magic_2(z)
return (z + 1)
x = 0
print(x)
perform_magic_1(x)
print(x)
perform_magic_2(x)
print(x)
x = perform_magic_3(x)
print(x)
x = perform_magic_4(x)
print(x)
SMU Classification: Restricted
COR-IS1704 – Lab 2
Functions
from 𝑚 to 𝑛.
You are given a function display_numbers in q2.py that displays all the integers ranging
Note: You do not need to understand how the function is implemented. In other words, you can ignore
Line 14 to Line 16 of the function, but you should read the description of the function, i.e., the docstring.
a) You can use the help() function to get the description of a function (which is in the triple quotes
and which is called docstring).
help(display_numbers)has been provided in line 25. Run the file and observe what
happens.
b) Uncomment the following code in line 32 and run the file again.
display_numbers(3, 5)
c) Write a piece of Python code from line 36 that calls the display_numbers function to
display the following output:
Q3 [ ** ]: Pattern Printer
You are given a function called print_pattern in q3.py.
Note that you do not need to understand how the function is implemented, (i.e., you do not need
to understand Line 16 to Line 20). We will study it a bit later. However, do read the docstring
provided.
Write a piece of Python code that uses this function to display the following output:
SMU Classification: Restricted
COR-IS1704 – Lab 2
Functions
Q4 [ ** ]: Interest Calculator
So far you have practiced how to use a given function. In this exercise you will practice how to
define your own function.
Please refer back to Lab1 - Q5 and define a function called calculate_interest that takes in 4
parameters:
principal: the amount of principal that's deposited.
annual_interest_rate: the annual interest rate.
frequency_of_compounding: the number of times the interest is compounded per year.
deposit_period: the deposit time (in years)
and returns the total interest earned by the end of the deposit period, rounded with 2 decimal
places. (Use the built-in function round(x, 2) to round x to 2 decimal places.)
Next, write a piece of code that prompts the user for his/her principal and the deposit time, and use
the calculate_interest function that you have written to help
you calculate and display the interest earned at the end of the deposit period.
Your code should produce the following output (similar to previous lab1):
and returns how much the customer has to pay for buying that particular quantity of items (after
discount).
For example, if the unit price of a ruler is $1.50, the quantity is 5 and the discount is 10%, then the
final price to pay will be 1.5 × 5 × (1 – 10 / 100), which is $6.75.
Part 2 [ * ]: Given the following items in the shopping cart with their unit price, quantity and
discount, call your function in Part 1 and calculate the value of the total payment after discount:
milk: $5.95, 2, 10%
rice: $6.50, 1, 5%
eggs: $2.40, 2, 0%
kaya: $3.95, 3, 15%
COR-IS1704 – Lab 2
Functions
Q6 [ * ]: Phone Bill
You're given the function compute_phone_bill in q6.py that computes the amount on a
monthly mobile phone bill. The function takes the following parameters:
Now the business rules for computing the monthly bills are as follows:
Based on the function compute_phone_bill provided, can you figure out the returned values of
the following function calls without running the code?
a) compute_phone_bill(35.5, 2, 800)
b) compute_phone_bill(35.5, 3)
c) compute_phone_bill(22.5)
d) compute_phone_bill(35.5, data_limit=2, amount_data=800)
e) compute_phone_bill(base=35.5, data_limit=2, amount_data=800)
f) compute_phone_bill(data_limit=2, amount_data=800, base=20)
g) compute_phone_bill(35.5, amount_data=800, data_limit=2)
h) compute_phone_bill(amount_data=800, data_limit=2)
i) compute_phone_bill(32, 2, 800)
j) compute_phone_bill(35.5, 2, 2050)
k) compute_phone_bill(35.5, 2, 1900, 10, 20)
l) compute_phone_bill(35.5, 2, num_minutes_extra_calls=100)
m) compute_phone_bill(35.5, 2, num_minutes_extra_calls=100, num_extra_sms=100)
Q7 [ ** ]: String Manipulation
COR-IS1704 – Lab 2
Functions
You are also given the starting code of a script called “q7.py”. Inside the file, there are two
functions that need to be implemented: q7_1 and q7_2.
Read the instructions provided in the docstring of these functions to know what is
expected from those functions.