[Tutor] Trouble with making a chart

Alan Gauld alan.gauld at btinternet.com
Mon Sep 29 14:19:06 CEST 2014


On 24/09/14 07:07, Martin Skåreby wrote:
> Hello!So i'm having trouble with a task i got from my teacher in a book. I'm supposed to create a chart which shows the converted numbers between Celsius and Fahrenheit. They want the Celsius to range from 40 to -40. I also get to know that Fahrenheit  = 32+celsius*9/5. Now I managed to write the number 40 to -40 from top to bottom. But i'm having trouble converting celsius to fahrenheit and printing it at the same time on the side of celsius. The chart should look something like this:Fahrenheit to Celsius=================Celsius       Fahrenheit40              10439              102,238              100,437              98,6etc.            etc.My current code is: print("Fahrenheit to Celsius")print("=======================")print("Celsius\t    Fahrenheit")for f in range(40,-41,-1):    print(f)for c in range(104,-41,-1):    print(32 + c * 9 / 5)Now this makes the numbers for Fahrenheit to just add below celsius. How do I make them go to the other side  under the text Fahrenheit? I'm
 doing this in iPython Notebook.

Please send emails in plain text. Yours has come out as one long line...

I'll try to reformat as best I can...

 > same time on the side of celsius. The chart should look something
 > like this:
 > Fahrenheit to Celsius
 > =================
 > Celsius       Fahrenheit
 > 40              104
 > 39              102

 > current code is:
 > print("Fahrenheit to Celsius")
 > print("=======================")
 > print("Celsius\t    Fahrenheit")
 > for f in range(40,-41,-1):
 >    print(f)
 > for c in range(104,-41,-1):
 >    print(32 + c * 9 / 5)

 > Now this makes the numbers for Fahrenheit to just add
 > below celsius. How do I make them go to the other side

The normal way to do this is store the results before
trying to print them.
Then you can use string formatting to print both
figures in a single line.

Something like this pseudocode:

results = []
for c in range(-41,40):
     f = (32+c*9/5)
     results.append( (c,f) )

print headers
for result in results:
     print("{}\t{}".format(result))


The notebook of IPython can do much cleverer things with
data but I'll ignore that for now and stick with
simple text tables.


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list