In this exercise,
you are going to be writing a function
that calculates how many cans of paint you need
in order to paint a wall,
given the height and width of the wall.
So what you're going to do is you're going write a function,
and it has to be called paint_calc
because the code on lines 10 to 13
makes sure that we get the inputs,
we work out the coverage of our paint,
and we use your function, paint_calc,
in order to output the printed statement
to tell the user how many cans of paint they need.
The way to work out the area of a wall
is to multiply the height by the width,
and then we divide that number by the coverage of the paint
in order to work out how many number of cans we need.
Now, because the shops don't sell you a fractional can,
and believe me, I've tried to ask for that,
you have to always round upwards,
y ou can't round down.
So even if it was 1.1 cans,
you will need to purchase two cans,
or so says the sales agent at my local home hardware store.
Now, take a look at the instructions,
you'll see that the Example Input and Example Outputs
and the format that they need to be.
In this case, we're saying that the first number
in the Input is the height and the second is the width,
but in reality it doesn't really matter
because area is worked out in either way,
height times width or width times height,
and it doesn't really matter.
But what it does matter
is to figure out how to round up a number,
and I've put a hint
into the instructions from Stack Overflow.
You could also search for your own ways
of figuring this out.
But, have a go at writing the code,
and hit Submit when you think you're ready,
and check it against the pre-made tests.
The first thing we need to do is to define our function.
So in Python, that is the "def keyword", space,
and then the name of the function.
And we said that it has to match the line
that we already created to call the function on line 12.
So it has to be called paint_calc.
And then we have a set of parentheses ()
where we can put the inputs to our function.
In this case, it's height, width, and the coverage.
Now, we haven't put any inputs into our function just yet
because remember, our function is a multi-use tool.
It's something that we can use again and again
by putting in different inputs.
So for example, the function len() can take any list,
and it will work out the length of the list
or how many items are in the list.
So the inputs can always change,
but we define in our function
how we want to work with those inputs.
After our definition for our function name and inputs,
we create the actual body of the function.
And we do that by indenting into the function.
The first thing we do is we calculate
the number of cans we need
by multiplying the height by the width to work out the area,
putting a parenthesis around this,
and then we divide it by the coverage of the paint.
This is going to be a decimal number in many cases,
so we have another line on line 5
where we round up the cans.
In order to round it up, we use the math module
and we use the math.ceil() method.
This is going to go to the ceiling of the number,
which basically means rounding it up
to the nearest whole number.
And we pass our number of cans
as the input into this function,
and then we calculate it and store it inside round_up_cans.
And then finally, we print out the message,
You'll need round_up_cans cans of paint,
inserting our number of cans of paint rounded up
into that print statement.
And the way that our function is going to work
is we've already defined our function on lines 3 to 6,
and then on line 12,
we actually call the function, passing in actual values.
So in this case, the first input as the height,
the second input as the width,
and the coverage as whatever is defined on line 11.
And when that line of code is run,
then we get our output printed into the output area.