Before session 3
Go to Moodle and take the third test concerning the video lecture.
The deadline for homework is Sunday. Nevertheless, you can start with homework already before the session.
This week we are going to get an insight into functions and definitions (def). Functions allow to perform tasks that are similar to each other and differ only by values of parameters. So there is no need to rewrite essentially the same algorithm many times, we can just call the function with right parameters.
The shoe size program is rewritten using a function called shoe_size that takes a foot length as its parameter and returns the suitable shoe size.
def shoe_size(length): size = 1.5 * (length + 1.5) return size name = input("Enter your name: ") foot = float(input("Enter your foot length (cm): ")) shoe = shoe_size(foot) print("Dear " + name + ", your suitable shoe size is "+str(round(shoe))) print("Shrek's shoe size is", round(shoe_size(42.3)))
The following example shows the function that takes two parameters (two numbers) and returns the greatest. In the example, the function is called twice; therefore, the results are stored in two variables. After that the smallest of two greatest is found. To show the efficiency of the function, it is tested on different inputs.
def maximum(a, b): if a < b: return b else: return a try: x1 = int(input("Enter the first number: ")) y1 = int(input("Enter the second number: ")) m1 = maximum(x1, y1) print("The input numbers are", str(x1), "and", str(y1) + ". The greatest number is", str(m1) + ".") x2 = int(input("Enter the third number: ")) y2 = int(input("Enter the fourth number: ")) m2 = maximum(x2, y2) print("The input numbers are", str(x2), "and", str(y2) + ". The greatest number is", str(m2) + ".") if m2 > m1: print("The first greatest number is smaller.") else: print("The second greatest number is smaller.") except: print("Please enter a number.") print("The input numbers are 2 and 5. The greatest number is", str(maximum(2,5)) + ".") print("The input numbers are 4 and 1. The greatest number is", str(maximum(4,1)) + ".")
Exercise 1. Number of days in months
Rewrite the number of days in months program using a function called number_of_days that takes the number of a month as its parameter and returns the number of days in that month.
Example of the function call:
>>>number_of_days(3) 31 >>>number_of_days(9) 30
Test the program on various input values.
Exercise 2. Price difference
Write a function called price_difference that calculates the difference between the price on the installment plan and the initial price of the product. The function should take three parameters:
- the initial price of the product
- monthly payment
- number of months on the installment plan.
Example of the function call:
>>>price_difference(200,10,30) 100 >>>price_difference(200,20,12) 40
Modify the program so that it asks the user for the initial price of the product and two different installment plans. The program has to compare the plans and output the plan which is the best for the user.
Example of the program output:
What is the price of the product? 200 What is the monthly payment on the first installment plan? 10 How many months does the first installment plan last? 30 What is the monthly payment on the second installment plan? 20 How many months does the second installment plan last? 12 The second installment plan is better!
Save the price difference (returned by the function) in a variable. To call the function twice, you will need two different variables. You can then compare these two values and use if-else to choose the best installment plan.
Go to Moodle and put your solutions into Exercises 3.