Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Define Variables

00:00 Next you’re asked to use string.shout() and calc.area() to print THE AREA OF A 5-BY-8 RECTANGLE IS 40.

00:09 There are different things you could do there. First, it is nice to dissect the output message first and create some variables for it, and then kind of weave in the string.shout() call and the calc.area() call for it.

00:26 THE AREA OF A 5-BY-8 RECTANGLE IS 40. That means we need some length, which is 5, and a width that is 8 because that’s a part of the message, like A 5-BY-8 RECTANGLE.

00:41 So that’s the two variables here. And if you remember the area() function, length and width are the parameters that you defined for this function.

00:50 So these are fitting names. And then we have a message, which is THE AREA OF A and then length by width RECTANGLE IS and then the product of them.

01:03 This should be uppercase. So you could be sneaky here and say, well, the message itself can be typed uppercase, but you are requested to also use string.shout().

01:14 So let’s not do the message in uppercase, but in—how do you call it?—like, normal case: "The area of a", and then you add in the variable length

01:29 dash by then another variable, which is width. So both is in curly braces because we will have a formatted string in a moment. I will adjust my screen a little bit so you can see the full message.

01:48 So I was saying that we are using a formatted string. You might have noticed currently it’s a normal string, so I will quickly jump at the beginning of the string and add an f before the quote so it’s an f-string. So you can actually use the curly braces inside the string and format the string with the length and the width variables.

02:08 So the area of a length by width

02:12 rectangle is, and for now, let’s just write 40 there.

02:18 Save the file. And at this moment, there is not much to run because we’re not calling something yet. So let’s call the print() function with message.

02:31 Save the file and run the file. And on the left side, you see The area of a 5-by-8 rectangle is 40. That sounds like the solution, but there are still a few steps you need to do.

02:45 A is that the 40 is hard-coded in the string right now. So if you would change the length and the width, there would still be 40 there as a product, which is not correct, and you have a function for that.

02:58 And also it’s not uppercase yet. So let’s tackle that in the next lesson.

Become a Member to join the conversation.