[Tutor] making a table

mailing list ml.cyresse at gmail.com
Fri Sep 9 12:25:14 CEST 2005


Hi Goofball223, 

Just a quick thing -

> for i in (0,10, 20, 30, 40, 50, 60, 70, 80, 90, 100):

Have you used range() before?

for i in range(10): 
     print i
 
0
1
2
3
4
5
6
7
8
9

It's handy for situations like yours. Also, you could use it like this - 

zeroToNine = range(10)
print zeroToNine 
[0,1,2,3,4,5,6,7,8,9]

for i in zeroToNine:
     print i

..Just a quick mention that range(x) counts up to, but not including
x. ( There's good reasons for this, which someone can explain if
needed.)

Also, by default it starts at zero, and increases by 1. You can change
these, however.

i.e. to create a list from 5 to 50 in increments of 5 - 

zeroToFifty = range(start = 5, stop = 55, step = 5)

Remember, it'll count up to but not including stop, so if you tell it
to count to two hundred in steps of 10, it'll count to 190, counting
to 16 by steps of 2 will count to 14. So always set it one step value
above what you want.

(start defaults to 0, step defaults to 1, hence you can just use
range(stop = 100) or
range(100) for short.)

You're looking good, however.

Cheers, 

Liam Clarke

On 9/9/05, Goofball223 at wmconnect.com <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() 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
>


More information about the Tutor mailing list