[Tutor] Principles of code reuse error (Nagaraj S)

Alan Gauld alan.gauld at yahoo.co.uk
Sat Nov 20 09:11:59 EST 2021


On 19/11/2021 21:51, Nagaraj S wrote:

> In this code, identify the repeated pattern and replace it with a function

You didn't show us the original code so the error might be something
beyond the parameter types.

> *days* in that month as parameters. Adapt the rest of the code so that the
> result is the same. 

Without seeing the code we can't tell what the original "result"
might have been.

> Confirm your results by making a function call with the
> correct parameters for both months listed.
> 
> def month_days(month, days):
>    print(month +" has " + days +" days")

Given the subject line I feel bound to point out that for reuse
your function should not print() the result but return it as
a string. This will make your function reusable in other
contexts such as a web page or GUI application. The current
version only works in a command line environment.

That change would then require you to print() the result
of the function in the main body of your code.

-- 
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