[Tutor] printing puzzlement

Rob rob@uselesspython.com
Mon, 12 Aug 2002 17:10:17 -0500


This has turned out to be a most interesting challenge to tackle so far.
It's turning into one in which I take other-than-the-best C++ code written
quickly by someone else, and transmuting it into something entirely
different in another language, while achieving the same output.

As much fun as this is to do, I think I'll wind up writing a few short C++
programs of my own that can be used as better examples. (Not that I'm the
world's finest C++ hacker, of course.) I did some of this to demonstrate
concepts (and plug Python) during this summer's C++ courses, and it seems to
have gone over well.

Rob

> -----Original Message-----
> From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
> alan.gauld@bt.com
> Sent: Monday, August 12, 2002 5:26 AM
> To: rob@uselesspython.com; tutor@python.org
> Subject: RE: [Tutor] printing puzzlement
>
>
> > As seen in the C++ code snippet here, calculations are
> > performed in the middle of the display of each line of
> > output. Does anyone  know of a good way to reproduce
> > similar behavior in a Python program?
>
> The same way the C++ programmer would have done it if
> [s]he'd had any sense - using a string buffer and then
> writing the full string out!
>
> In Pythons case use a format string and some variables
> to hold the intermediate values...
>
> > 	// Display the amortization schedule
> > 	for (int index = 0; index < numPayments; index++)
> > 	{
> > 		cout << setw(5) << right << (index + 1);
> > 		cout << setw(12) << right << fixed <<
> > setprecision(2) << principle;
> > 		cout << setw(9) << right << fixed << amtPayment;
>
> buff = "%5d%12.2d%9d" % (index+1,principle, amtPayment)
>
> > 		float interest = (float (int (principle *
> > monthRate * 100) ) ) / 100.0;
> > 		cout << setw(11) << right << fixed << interest;
> > 		principle = principle + interest - amtPayment;
> > 		cout << setw(12) << right << fixed << principle;
>
> buff = buff + "%11d%12d" % (interest, principle)
>
> > 		cout << endl;
>
> print buff
>
> The types and precisions might need frigging but
> you hopefully get the idea I'm suggesting.
>
> Alan g.
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>