[Tutor] (no subject)

Alan Gauld alan.gauld at yahoo.co.uk
Tue Sep 8 04:54:28 EDT 2020


On 08/09/2020 06:02, Korean Panda wrote:
> Attached is a screenshot of the exercise. So I'm needing help with with
> this. I'm getting the code to print the area, but for the purpose of
> cleaning up the code style, these commands are still incorrect. Any ideas?

I'm not sure how you determine what "correct" is but the biggest comment
I would make is that best-practice suggests that you separate output
from calculations. So it would be better for your function to return
the area and then the caller of the function to print the result.

> def rectangle_area(base, height):
>     area = base*height # the area is base*height
>     print("The area is " + str(area))
> 
> rectangle_area(4, 3)
> 
> Here is your output:
> The area is 12

Another point is that in your call to print you convert area to
a string and add it to the other string. But print() does all
of that for you, you only needed to write:

print("The area is", area)

The only other style issue I can see is that your comment
is not very helpful, it simply tells us what the code already
says. Comments should explain why things are the way they
are, not simply repeat the code. In a function this trivial
there should not be any need for comments, good variable
names should suffice.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list