Before session 3
Functions
Watch the videos. Next two videos (part 1 and part 2) show how Python implements the 'store and use later' programming pattern.
Slides
Text-book: chapter 4 - functions
Test
Go to Moodle and take the third test concerning the video lecture.
Homework
The deadline for homework is Thursday, so start with homework 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.
Example 1
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)))
Example 2
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)) + ".")
Homework 3.1 Divisibility 2
Rewrite the is divisible by 3 and 5 program using a function called is_divisible. The function takes 2 arguments - the numbers that the user input will be checked with for division (see examples below).
Examples of function calls:
>>> is_divisible(2,7)
Enter a number: 14
Number 14 is divisible by 2 and 7
>>> is_divisible(3,5)
Enter a number: 9
Number 9 is divisible by 3
>>> is_divisible(3,5)
Enter a number: hello world
The inserted value was not a number
>>> is_divisible(1,2)
Enter a number: 4
Number 4 is divisible by 1 and 2
>>> is_divisible(1,2)
Enter a number: 4
Number 4 is divisible by 1 and 2
Test the function with various input values.
If you have been struggling with this exercise for some time, then maybe you can get help from the troubleshooter which tries to explain most common problems and give hints.
Homework 3.2 Customer card profitability
Write a function called customer_card_discount that calculates if it would be profitable to buy a customer card for a discount or not (meaning that the purchase cost after discount + card price is less or equal to the original cost). The function should take the following arguments:
- total purchase amount in euros
- discount percent with customer card
- customer card price
Use the following formula to calculate the price with the discount and the card price:
new price = (total purchase amount) * (100 - discount)/100 + (customer card price)
If the card saves the customer some x amount of euros then the function should return x. If purchase + card price is more expensive than the original purchase price then return -x, where x is the number of euros it’s cheaper with the card (see examples below).
Examples of function calls:
>>> customer_card_discount(200, 5, 5)
5.0
>>> customer_card_discount(200, 0, 5)
-5.0
Modify the program so that it asks the user for the total purchase amount in euros, discount percent, and customer card price. The program has to compare the prices and tell the user if it’s a good idea to get the card.
Examples of program output:
>>> %Run solution.py
Total purchase amount: 200
Discount percent: 5
Customer card price: 5
It’s better to get a card, you will save 5 euros.
>>> %Run solution.py
Total purchase amount: 100
Discount percent: 5
Customer card price: 15
It’s better to pass on the customer card, it will be 10 euros cheaper.
Hint
Save the price discount amount (returned by the function) in a variable. You can then check that value using if-else to figure out whether the customer card would be profitable.
If you have been struggling with this exercise for some time, then maybe you can get help from the troubleshooter which tries to explain most common problems and give hints.