Take the 2-minute tour ×

I am working in python 2.6. Can anyone figure out why my variable squared does not calculate properly when it is inside a min() function? To illustrate, I show the variable**2 performed by itself, works fine. Here is the code, followed by some results:

from __future__ import with_statement
import csv, re, time
timestr = time.strftime("%Y%m%d_%H%M%S")

fileinput = 'mp_20130822.csv'
out = open('output2.csv',"wb")
writer = csv.writer(out, delimiter = ',')

ESPmax = 100.00
dynmax = "enabled"

def config():
    global argument
    if z>30:
        argument = "T"
        argument = "F"

with open(fileinput,'rU') as inputfile:
    reader = csv.reader(inputfile, delimiter = ';')
    for line in reader:
            line = re.split(",| ",line[0])
            side = str(line[4]) #is "Bid" or "Ask"
            e = float(line[5])
            z = float(line[6])
            t = float(line[33])
            FW = float(line[34])
            FS = max(float(line[35]),200)
            if FW == 0:
            if (FS == 0) or (FS == 1):
            if side == "Ask":
                LE = t-e
                LE = e-t
            LEP = LE/(FW/2)
            ESP = z/(FS/2)
            if dynmax == "enabled":
                ESPmax = min(LEP**2,ESPmax)

    if (argument == "T"):
        print ('side, e, z, t, FW, FS')
        print ('LEP,LEP,ESPmax')
        print (side, '%.2f'%e, '%.2f'%z, '%.2f'%t, '%.2f'%FW, '%.2f'%FS)
        print ('%.3f'%LEP,'%.3f'%LEP,'%.5f'%ESPmax)
        print '%.5f'%(LEP*LEP)


side, e, z, t, FW, FS
('Ask', '1.90', '50.00', '1.95', '0.24', '651.00')
('0.423', '0.423', '0.00130')
side, e, z, t, FW, FS
('Ask', '8.40', '40.00', '8.43', '0.17', '4933.00')
('0.348', '0.348', '0.00130')
side, e, z, t, FW, FS
('Ask', '8.40', '40.00', '8.43', '0.17', '4919.00')
('0.370', '0.370', '0.00130')
share|improve this question
Seems to work fine, ESPmax = min(LEP**2,ESPmax) seems to have set ESPmax to a value smaller than any of the LEP**2 you printed out, so of course the minimum is the smaller value 0.00130 you probably got from an LEP of 0.036 earlier. –  Daniel Fischer Aug 22 '13 at 20:33
how could ESPmax have been reduced to 0.00130? –  user2136314 Aug 22 '13 at 21:30
By an LEP value of approximately 0.036 earlier. Since it's extremely unlikely that min or **2 don't work correctly, and you always get the same value in what you pasted here, it's overwhelmingly likely that something changed the value of ESPmax to 0.00130 before. Note that you only print anything if Z > 30 (which, by the way, doesn't seem to be defined where it's used in config()). –  Daniel Fischer Aug 22 '13 at 21:35
it does seem that ESPmax is getting reset to a random, fixed number after I set it to 100. I have no clue why. I ran my code again and this time it is getting set to 0.0. –  user2136314 Aug 22 '13 at 21:55
No, select isn't broken. ESPmax is getting set by your code. You don't know why, so debug it. I don't know if there's a decent debugger for Python, but old-skool printf-debugging works everywhere [except on embedded systems where you have no console to print stuff out]. Print out the values of ESPmax and LEP before the line ESPmax = min(LEP**2,ESPmax). You may need to look at other values too, but start with these. –  Daniel Fischer Aug 22 '13 at 22:04
show 1 more comment

1 Answer

The indentation is clearly a SyntaxError in Python, so its unclear what your code actually does. Though I'm pretty sure that you somehow manage to assign 0.00130 to ESPmax, and once you get there the

ESPmax = min(LEP**2,ESPmax)

line will not change ESPmax for your values of LEP.

share|improve this answer
But how could ESPmax have been reduced to 0.00130? I initially gave it a default value of 100. The only time it's assigned a new value is here: ESPmax = min(LEP**2,ESPmax). Also not sure there is an indentation error, or where you see it. The code did not return a syntax error... –  user2136314 Aug 22 '13 at 21:29
In the code here, the config() call doesn't match any previous indentation level. Probably an artifact from pasting it here, did you perchance use tabs to indent? Also not unlikely that you pasted the code here and forgot to select the last lines when you indented the entire code block. –  Daniel Fischer Aug 22 '13 at 21:40
yeah, I see it now. probably was an artifact from pasting. So, I figured out my problem. it was so silly. since I am defining ESPmax inside the loop, I forgot to reset its value to the default at the end of the loop. so in the next iteration of the loop, the default value became the previous calculated value. if it was a low number. It kept on getting smaller through the loop whenever a new lower value was passed through the min() function. Thank you –  user2136314 Aug 22 '13 at 22:02
add comment

Your Answer


