[Tutor] Testing print

boB Stepp robertvstepp at gmail.com
Sat Oct 1 11:12:10 EDT 2016


On Sat, Oct 1, 2016 at 2:02 AM, Alan Gauld via Tutor <tutor at python.org> wrote:
> On 01/10/16 05:24, boB Stepp wrote:
>
>> ===============================================================================
>> '''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):
>      <snip>
>> 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))
>
> Do you need print_msgs()?
> Won't it work the same with
>
>            print(right_justify(input_string))
>
> You are only feeding one line at a time into the print msgs.

[snip]

> But I think I'd just leave it as you have it but
> without the print_msgs()...

I would still need to unpack the arguments returned by right_justify()
that get fed to print():

print(*right_justify(input_string))

And I would have to add an additional "\n" to msg in the else clause
of right_justify():

 msg = ("The string has too many characters (> 70)!\n" +
            "Only a partial, 70 character line will be returned.\n")

so that the result formats the same as the original intent.  This
gives me main() now as:

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(*right_justify(input_string))

Doing this did not occur to me because I was blind to treating
print(*args) like any other function in regards to argument unpacking.

One of the many wonderful things about Python is its consistency!

Thanks, Alan!



-- 
boB


More information about the Tutor mailing list