views:

69

answers:

1

For some reason this program is saying that 'switch' is not defined. What is going on?

#PYTHON 3.1.1
class mysrt:
        def __init__(self):
            self.DATA = open('ORDER.txt', 'r')
            self.collect = 0
            cache1 = str(self.DATA.readlines())
            cache2 = []
            for i in range(len(cache1)):
                if cache1[i] == '*':
                    if self.collect == 0: self.collect = 1
                    elif self.collect == 1: self.collect = 0
                elif self.collect == 1:
                    cache2.append(cache1[i])
            self.ORDER = cache2
            self.ARRAY = []
            self.GLOBALi = 0
            self.GLOBALmax = range(len(self.ORDER))
            self.GLOBALc = []
            self.GLOBALl = []
        def sorter(self, array):
            CACHE_LIST_1 = []
            CACHE_LIST_2 = []
            i = 0
            for ORDERi in range(len(self.ORDER)):
                for ARRAYi in range(len(array)):
                    CACHE = array[ARRAYi]
                    if CACHE[self.GLOBALi] == self.ORDER[ORDERi]:
                        CACHE_LIST_1.append(CACHE)
                    else:
                        CACHE_LIST_2.append(CACHE)
                for i in range(len(CACHE_LIST_1)):
                    if CACHE_LIST_1[0] == CACHE_LIST_1[i] or range(len(CACHE_LIST_1)) == 1:
                        switch = 1
                        print ('1')
                    else:
                        switch = 0
                        print ('0')
                        break
                if switch == 1:
                    self.GLOBALl += CACHE_LIST_1 + self.GLOBALc
                    self.GLOBALi = 0
                    self.GLOBALc = []               
                else:
                    self.GLOBALi += 1 
                    self.GLOBALc += CACHE_LIST_2
                    mysrt.sorter(CACHE)
            return (self.GLOBALl)

                #GLOBALi =0
                    # if range(len(self.GLOBALc)) =! range(len(self.ARRAY))
    array = ['ape', 'cow','dog','bat']
    ORDER_FILE = []        
    mysort = mysrt()       
    print (mysort.sorter(array))
+6  A: 

If CACHE_LIST_1 is an empty sequence then switch will never get bound.

Ignacio Vazquez-Abrams
Wouldn't the else cover that?
Protean
The `else` is part of the `if`. If the `if` never gets reached then there's no way for the `else` to take effect.
Ignacio Vazquez-Abrams
No - the entire `for` loop would never run, thus never reaching the `if` clause.
Yuval A
Ok, when I attempt to just add switch = 1 before the switch check the sorter class gives an error saying that 2 arguments are taken and only 1 is given.
Protean
If <code>len(self.ORDER)</code> == 0, then <code>for i in range(len(CACHE_LIST_1))</code> will never be executed and the <code>else</code> will never be executed. So the <code>else</code> doesn't cover that
inspectorG4dget
@Protean: please post the actual code and error message. That would be easier to respond to
inspectorG4dget