-2

I have a program code to calculate the average of input txt file, and it will save the average value to baseline. When user execute the program second time, it should calculate another average of txt file, the average will compare the baseline.

def memory_baselineValues():
memory_txtfile = file_memory_locat.get() ### Enter txt file name 
infile = open(memory_txtfile, 'r')
stuff = infile.readlines()
infile_memory_average = (int(stuff[3]) + int(stuff[13]) + int(stuff[23]) + int(stuff[33]) + int(stuff[43]) + int(stuff[53]) + int(stuff[63]) + int(stuff[73]) + int(stuff[83]) + int(stuff[93])) / 10 ###calculate the average of txt file values
average_memory.insert(INSERT, infile_memory_average) ### output the average of txt file values
baseline_average = txt_memory_baseline.get()
if baseline_average == "":   ###Check the textbox "txt_memory_baseline" values is null or not null, first time execute should null
    baseline_average = infile_memory_average
    txt_memory_baseline.insert(INSERT, baseline_average) ### add the first time execute average values to textbox
    var = tkMessageBox.showinfo("Alert", "Test")
    exit
elif infile_memory_average > baseline_average:
    var = tkMessageBox.showinfo("Alert", "The average values of "+memory_txtfile+" is over the baseline")
    read_average = infile_memory_average
    before_baseline = int(baseline_average)
    baseline_average = (read_average + before_baseline) / 2 ###update baseline
    txt_memory_baseline.delete(0, END)
    txt_memory_baseline.insert(INSERT, baseline_average)
    exit
elif infile_memory_average < baseline_average:
    var = tkMessageBox.showinfo("Alert", "Safe")
    read_average = infile_memory_average
    before_baseline = int(baseline_average)
    baseline_average = (read_average + before_baseline) / 2 ###update baseline
    txt_memory_baseline.delete(0, END)
    txt_memory_baseline.insert(INSERT, baseline_average)

    exit
else:
    print"Somethings wrong"
print "done" 
infile.close()

Assume my first value is 506, since it is first time execute, the if statement if baseline_average == "":should be run. The problem is when the second value is 7099, it is higher than baseline and should be run with elif infile_memory_average > baseline_average: but my program is only run elif infile_memory_average < baseline_average:every times. What is the logic error here?

Tommy Lam
  • 25
  • 1
  • 2
  • 4
  • What is "average of a txt file" supposed to mean? – timgeb Mar 21 '17 at 07:36
  • Put a breakpoint just before elif infile_memory_average > baseline_average: and check both the values for the second time execution. – Abijith Mg Mar 21 '17 at 07:37
  • http://stackoverflow.com/questions/3270680/how-does-python-compare-string-and-int – Lafexlos Mar 21 '17 at 07:40
  • I don't see how this program can possibly run. You compare `baseline_average` to a string, and later you compare it to an integer. What is it really, and where is the function `txt_memory_baseline.get`? – Paul Cornelius Mar 21 '17 at 07:47

1 Answers1

0

From what I can see, I think you need to declare your baseline_average as an integer. If you are reading it as a string instead, the value of it will be different. so try this:

after: baseline_average = txt_memory_baseline.get()

do: baseline_average=int(baseline_average)

That would be my guess.

alex
  • 152
  • 11