Parcel cost function
A neighbourhood courier charges 40 base units, plus 8 per whole kilogram and 2 per kilometre. Read weight and distance, define a function named delivery_cost, and print the calculated charge.
Input
One line containing two integers: weight in kilograms and distance in kilometres.
Output
One integer: the total delivery charge.
Example 1
Input
3 12
Output
88
40 + (3 × 8) + (12 × 2) equals 88.
Constraints
- 1 <= weight <= 100
- 1 <= distance <= 1000
Hints
Hint 1 of 3
A function sends a value back with return.
Hint 2 of 3
Calculate each variable part before adding the base charge.
Hint 3 of 3
Return 40 + weight * 8 + distance * 2.
Solution
Show a reference solution and explanation
def delivery_cost(weight, distance):
return 40 + weight * 8 + distance * 2
weight, distance = map(int, input().split())
print(delivery_cost(weight, distance))
Why it works
The named function separates the pricing rule from input and output. Its parameters make the calculation reusable, and returning the number lets the caller decide how to display or further process it.
Lesson for this exercise: Functions in Python