[Tutor] calling a variable name

Kent Johnson kent37 at tds.net
Mon Oct 22 13:46:17 CEST 2007


Bryan Fodness wrote:
> Here is the actual snippet of code
>  
> 
> calc_depth =    8.1         # which is actually d
> unblockedFS =   13.4     # which is the indexed fs 
> 
> for line in file('21Ex6MV_tmr.dat'):
>     d, fs1, fs2, fs3, fs4, fs5, fs6, fs7, fs8, fs9, fs10, fs11, fs12, 
> fs13, fs14, fs15, fs16, fs17, fs18, fs19, fs20, fs21, fs22, fs23, fs24, 
> fs25, fs26, fs27, fs28, fs29, fs30, fs31, fs32, fs33, fs34, fs35, fs36, 
> fs37, fs38, fs39, fs40 = line.split()
>     if float(d) == round(calc_depth):
>         print float('fs' + str(int(round(unblockedFS))))

I think I would just keep the data in a list and index it:

for line in file('21Ex6MV_tmr.dat'):
     data = line.split()
     d = data[0]
     if float(d) == round(calc_depth):
         print float(data[int(round(unblockedFS))])

Kent


More information about the Tutor mailing list