Homework for Week 4
After this week you can
- Define new functions and call them in your program
- Make the distinction between returning a value and printing it
- Use built-in functions from Python standard modules
Functions
We can gather a collection of statements together and give them a short name. Then we don't need to write these statements again every time we want to use them, but instead write only the name. This can shorten the program considerably. Statements with a name are a function. There are two aspects of using a function in a program: function definition and function call. After defining a function, we can call it many times. Like the mathematical functions, the program's functions can have parameters and return values.
Watch the videos about defining and using functions in Python:
Slides in English
Textbook in English
Quiz
Go to Moodle and solve the quiz about functions. You can attempt the quiz several times, the best attempt counts.
Examples
Functions allow performing 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 the right parameters.
Shoe size function
Rewrite the shoe size program using the function called shoe_size that takes foot length as its parameter and returns shoe size.
def shoe_size(length): size = round(1.5 * length + 2) return size name = input("Enter your name: ") foot = float(input("Enter your foot length (cm): ")) shoe = shoe_size(foot) print("Dear " + name + ", your shoe size is " + str(shoe)) print("Shrek's shoe size is", shoe_size(42.3))
First, we define the function shoe_size, then call it twice.
Note that we convert the shoe size to a string in the first printing command: this is needed because only strings can be joined together using the + operator. When no concatenation of strings is needed, as in the second printing command, we can also omit the type conversion.
Maximum
The following example shows the function that takes two parameters (two numbers) and returns the largest of them. In the example, the function is called twice; therefore, the results are stored in two variables. After that, the smallest of the two largest is found. To show the effect of the function, we test it on different inputs.
def maximum(a, b): if a < b: return b else: return a try: x1 = int(input("Enter first number: ")) y1 = int(input("Enter second number: ")) m1 = maximum(x1, y1) print("Input numbers are", str(x1), "and", str(y1) + ". The largest is", str(m1) + ".") x2 = int(input("Enter third number: ")) y2 = int(input("Enter fourth number: ")) m2 = maximum(x2, y2) print("Input numbers are", str(x2), "and", str(y2) + ". The largest is", str(m2) + ".") if m2 > m1: print("The largest number in first pair is smaller.") else: print("The largest number in second pair is smaller.") except: print("Please enter a number.") print("Input numbers are 2 and 5. The largest is", str(maximum(2, 5)) + ".") print("Input numbers are 4 and 1. The largest is", str(maximum(4, 1)) + ".")
Exercises
1. Bakery cash register
A bakery produces three types of cakes: chocolate cakes (0.05 € / cm2), strawberry cakes (0.04 € / cm2), and Napoleon cakes (0.08 € / cm2). Write a function called cake_price, which takes cake name and cake size as parameters and returns the price of the cake in euros, rounded to two decimal digits. Chocolate cake and strawberry cake are round and their sizes are entered as radius. Napoleon cake is square-shaped and its size is entered as a side length. If the cake type is something else, then the function should return -1.
>>> cake_price('Napoleon cake', 10)
8.0
>>> cake_price('chocolate cake', 5)
3.93
>>> cake_price('bisquit cake', 10)
-1
Cake names are 'chocolate cake', 'strawberry cake' or 'Napoleon cake'. Size is given in centimeters.
2. Price difference
Write a function price_difference, which calculates the difference of total cost on the installment plan and the initial price of the product. The function should take three parameters in the following order:
- initial price of the product
- monthly payment
- number of months on the installment plan
Example of function call:
>>> price_difference(200, 10, 30)
100
>>> price_difference(200, 20, 12)
40
Then write a program that asks the user for the product's initial price and parameters of two different installment plans. By calling the function price_difference, the program compares the plans and outputs the plan, which is better for the user.
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!
You can assume that all numbers are integers.
3. Einstein's Theory of Special Relativity
If two bodies are moving in the same direction, where the speed of the first body with respect to the observer is u and the speed of the second body with respect to the first body is v, then according to the special theory of relativity, the speed of the first body with respect to the observer is calculated from the formula
{$$\frac{u+v}{1+\frac{u\cdot v}{c^2}},$$}
where c is the speed of light (299792.458 km/s). This expression is called the sum of speeds u and v.
Create a function called einsum, whose parameters are speeds u and v, and which returns the sum of these speeds according to the special theory of relativity.
>>> u = 100000
>>> v = 200000
>>> einsum(u, v)
245392.74884785622
Use this function to calculate the sum of speeds of four bodies that move in the same direction. The program should prompt for four speeds and print out their sum.
Enter speed of the first body with respect to the observer: 100000
Enter speed of the second body with respect to the first: 150000
Enter speed of the third body with respect to the second: 200000
Enter speed of the fourth body with respect to the third: 250000
Sum of speeds is 297993.41836837644 km/s
Hint. Add first two speeds. Then add the third speed to the sum. Then add the fourth speed to the sum.
Submit your solutions
Submit your solutions of exercises 1, 2, 3 to the autotester in Moodle under the names home1.py, home2.py, home3.py, respectively.