[Tutor] making a table

bob bgailer at alum.rpi.edu
Fri Sep 9 04:46:54 CEST 2005


At 04:34 PM 9/8/2005, Goofball223 at wmconnect.com wrote:
>I would like to construct a table for my program but it does not seem to 
>be coming out evenly. Could someone please let me know what to do so that 
>everything will work out correctly?
>
>def main():
>    print "This program shows a table of Celsius temperatures and there 
> Fahrenheit equivalents every 10 degrees from 0C to 100C"
>    print "C F"
>    for i in(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100):
>        fahrenheit = (9.0/5.0) * i + 32
>        print i
>        print "   ",
>        print fahrenheit
>
>main()

1 - you are getting C and F on different lines because there is no , after 
print i. After you fix that you will get:
C F
0     32.0
10     50.0
... similar lines deleted
40     104.0
... similar lines deleted
100     212.0

Now the trick is to get things lined up. Here % formatting comes in:

print "  C       F"
...
     print '%3s    %5.1f' % (i, farenheit)

Giving:
   C      F
   0     32.0
  10     50.0
etc. 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050908/bd1759a8/attachment.html


More information about the Tutor mailing list