On Fri, Sep 30, 2016 at 5:07 AM, Steven D'Aprano <st...@pearwood.info> wrote:

[snip]

> and preferably three:
>
> (1) function that does the calculation;
> (2) function that does the output;
> (3) function that calls (1) and then (2)
>
>
> If (1) and (2) are well-designed, then (3) is so trivial it needs no
> tests:
>
> def main():
>     x = calculate(stuff)
>     report(x)
>
> but of course it's not always that simple. Nevertheless, that's the
> ideal you should aim for.

OK, I want to make sure I have this thoroughly comprehended.  I have
rewritten my once simple program into the following:

===============================================================================
'''Exerise 3.1 from "Think Python 2" by Allen Downey.

This module will take a string and right justify it so that the last character
of the line will fall in column 70 of the display.  The results will be
printed to stdout.'''

def right_justify(a_string):
    '''This fucntion will take the string, "a_string", and right justify it by
    padding it with spaces until its last character falls in column 70 of the
    display.  If "a_string" has more than 70 characters, then "a_string" will
    be truncated to 70 characters.  The padded or truncated string will be
    returned along with a message if "a_string" is truncated.'''

    if len(a_string) <= 70:
        num_pad_spcs = 70 - len(a_string)
        return ((' ' * num_pad_spcs) + a_string),    # Note trailing comma.
    else:
        msg = ("The string has too many characters (> 70)!\n" +
            "Only a partial, 70 character line will be returned.")
        return msg, a_string[:70]

def print_msgs(*msgs):
    '''Prints messages to stdout.'''

    for msg in msgs:
        print(msg)

def main(input_strings):
    '''Run main program.'''

    print('0123456789' * 7)    # Print a number guide to check length of line.
    for input_string in input_strings:
        print_msgs(*right_justify(input_string))


if __name__ == '__main__':
    input_strings = [
            "Monty Python",
            "She's a witch!",
            "Beware of the rabbit!!!  She is a vicious beast who will rip"
            " your throat out!",
            ""]
    main(input_strings)
=============================================================================

Does this meet the criteria of each function doing its specific thing?
 Or is there something else I need to decouple/improve?

-- 
boB
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to