monkelijah
monkelijah

Reputation: 3

Python -- Why does a conditional execute in one function but does not in another?

I'm a beginner Python learner. I'm trying to write some basic code to automate a simple but repetitive work process, but got stumped by this problem.

# Prior to the code block below, a csv file import would have created the a and ag variables with their respective numerical values.

high = list(range(66, 99))
mod = list(range(36, 65))
low = list(range(1,35))
verysiggap = list(range(61, 99))
siggap = list(range(31, 60))
nonsiggap = list(range(1, 30))

fco_output_list = []

def fco_block1(*args):
    if a in high and ag in high:
        print("Both a and ag are high!")
        fco_output_list.append("fco1")
    if a in high and ag in high and a-ag in nonsiggap:
        fco_output_list.append("foc2")

fco_block1(a, ag)

a_ag_combi_output = 0
a_ag_gap_output = 0

def fco_block2(*args):
    global a_ag_combi_output
    if a in high and ag in high:
        a_ag_combi_output = 0
    elif a in high and ag in low or ag in mod:
        a_ag_combi_output = 1
    elif a in mod and ag in low:
        a_ag_combi_output = 2
    elif ag in high and a in low or a in mod:
        a_ag_combi_output = 3
    elif ag in mod and a in low:
        a_ag_combi_output = 4
    elif a in low and ag in low:
        a_ag_combi_output = 5
    global a_ag_gap_output
    if a in high and ag in high and a-ag in nonsiggap:
        a_ag_gap_output = 1

fco_block2(a, ag)

# There are some simple print functions after the code above that tell me very clearly that fco_block2 executed without any issues. 

As for the execution of fco_block1, there are no error messages but the conditionals don't seem to be executing. Based on the imported values of a and ag, the fco_output_list should have been populated by fco1 and fco2, but it is not.

Why is this so? I find it bizarre especially when the conditionals in block1 are worded in the same way as the respective conditionals in block2, but those in block2 got executed and not those in block1?

Upvotes: 0

Views: 62

Answers (2)

TookieWookie
TookieWookie

Reputation: 382

Your problem seems to be with the way you have defined the "high" list.

According to your comment: "I ran it umpteenth times with the same csv file (where a = 99, ag = 86)"

In Python the end values for "range" are exclusive (i.e. up to but not including), meaning that "list(range(66, 99))" will end in 98. Just to be clear, these are all the numbers in the "high" list:

[66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98]

As you can see, 99 is not there. So, for example, (a = 99, ag = 86) will fail because a is not in "high".

I tested your code with (a = 70, ag = 67) and it printed "Both a and ag are high!"

If you want ( a = 99, ag = 86) to trigger the print statement in fco_block1, change high to

high = list(range(66, 100))

Upvotes: 2

yuvi
yuvi

Reputation: 18427

I don't know what's wrong, because your logic is ok. It's kinda bizarre and all over the place, but it's functional. The problem is probably elsewhere, either in the part where you process the data later or input the data. Whichever, I made a little cleaner more pythonic version, where one function does everything and the logic is way more readable. I also change ag into b because it makes more sense to me. I didn't test it, honestly, but I hope that a more readable code would help you locate the problem better

high       = list(range(66, 99))
mod        = list(range(36, 65))
low        = list(range(1,35))
verysiggap = list(range(61, 99))
siggap     = list(range(31, 60))
nonsiggap  = list(range(1, 30))

fco_output_list = []
a_b_gap_output = 0

def populate_fco(*args):
    if a in high:
        if b in high: #only check this once! 
            print("Both a and ag are high!")
            fco_output_list.append("fco1") #populate the list first

            if a-b in nonsiggap: #do the other check
                fco_output_list.append("foc2")
                a_b_gap_output = 1

            return 0 #finally return a result
        if b in low or b in mod:
            return 1 

    elif a in mod and b in low:
        return 2

    elif b in high:
        if a in low or a in mod:
            return 3

    elif b in mod and a in low:
        return 4

    elif a in low and b in low:
        return 5

#instead of this variable being used a million times, I just returned the result through the function while also populating the list
a_b_combi = populate_fco(a, b)

Upvotes: 0

Related Questions