You are on page 1of 10

lOMoARcPSD|21839361

C105 P06 KARE - Lecture notes

Coding (Republic Polytechnic)

Studocu is not sponsored or endorsed by any college or university


Downloaded by Khushi Chaudhry (khushichaudhry26@gmail.com)
lOMoARcPSD|21839361

P06 GENERIC FUNCTIONS


Knowledge Attainment Review Exercise (KARE)

Downloaded by Khushi Chaudhry (khushichaudhry26@gmail.com)


lOMoARcPSD|21839361

Q1: Smaller numbers


a. Write a function smaller_num that takes in
two number parameters to determine and
return the smaller number of the two.

b. Write a program to do the followings:


• Ask user for two numbers
• Use the function to determine the smaller number and
display the result.

Sample Output 1 >

Sample Output 2 >

Downloaded by Khushi Chaudhry (khushichaudhry26@gmail.com)


lOMoARcPSD|21839361

Q2a: Buffet Price


• The cost of the buffet meal is based on the age and gender of
the person. See table below:

Age\Gender Male Female


>= 12 20 18
< 12 12 10

• Write / Define a function named getPrice that takes in gender


and age parameters to determine and return the price that a
person has to pay for the meal. Sample output of calling the
function in Wing IDE Shell is given below.
Sample Output 1 >

Sample Output 2 >

Downloaded by Khushi Chaudhry (khushichaudhry26@gmail.com)


lOMoARcPSD|21839361

Q2b: Total Buffet Price


Write the code to do the following
• Calculate the total cost for a family outing with the
following members using the getPrice function that you
have defined in the Q2a.
• Display the total cost for the buffet meal.

Member Gender Age


Peter “Male” 42
Mary “Female” 42
Alan “Male” 11
John “Male” 10

Sample Output >

Downloaded by Khushi Chaudhry (khushichaudhry26@gmail.com)


lOMoARcPSD|21839361

Q3: Area of Circle & Triangle


• Write/define a function area_circle that takes in the
radius parameter to calculate and return the area of a
circle.
(Note: You can take pi to be 3.142)

• Write/define a function area_triangle that takes in the


base and height parameters to calculate and return
the area of a triangle.
(Note: Area of triangle = (base * height) /2)

• Write the codes to use the above functions to


calculate the total area of the shaded region in the
figure below and print the result. Expected Shell
Output is given below Base = 10, Height = 5
Base = 3, Height = 2
Radius = 6
Downloaded by Khushi Chaudhry (khushichaudhry26@gmail.com)
lOMoARcPSD|21839361

Q4: Explain the Code


a) Explain what does the follow python code do by answering
the questions below:

i. What is the name of the function?


ii. What is/are the parameter(s) of the function?
iii. What does the function do?
iv. What is the value returned by the function?

a) Write the code to call the function in a python file.

Downloaded by Khushi Chaudhry (khushichaudhry26@gmail.com)


lOMoARcPSD|21839361

Q5: Explain the code


Explain what does the follow python code do by answering
the questions below:

i. What is the name of the function?


ii. What is/are the parameters(s) of the function?
iii. What does the function do?
iv. What is the value returned by the function?

Downloaded by Khushi Chaudhry (khushichaudhry26@gmail.com)


lOMoARcPSD|21839361

Q6: Catch the bug


With reference to the function given in Q5, explain what causes the
errors based on the scenarios given below and provide the correct
code to call the function without causing errors:
a. Scenario a:

b. Scenario b:

Downloaded by Khushi Chaudhry (khushichaudhry26@gmail.com)


lOMoARcPSD|21839361

P07 Preparation
• Go to the Youtube video “Python for Informatics - Chapter
8 – Lists” via the following link:
https://www.youtube.com/watch?v=nO8eU3uts0o&index=23&list=PLlRFEj9
H3Oj4JXIwMwN1_ss1Tk8wZShEJ
• Refer to PDF-Book “Python for Informatics.pdf” in
Resource folder of P01 problem package. Go through the
following Chapters & sections:
• Chapter 8 List – Section 8.1. to 8.2

Downloaded by Khushi Chaudhry (khushichaudhry26@gmail.com)

You might also like