Unable to find a logical error using my Python comparison program

I am trying to write a simple Python program that will connect to a small microcontroller alarm project that I am working on. The microcontroller is connected to eight switches. It outputs a binary value based on the state of the switch through the serial port.

I am trying to write a python program decoder and use hardcoded values ​​to test my logic. Here is what I wrote:

switches='11011101'
currentstate = {}
prevstate = {}


def initswitches():
        for x in range (0,8):
                name = "switch" + str(x)
                currentstate[name] = switches[x]
                prevstate[name] = switches[x]

def setswitches():
        for x in range (0,8):
                name = "switch" + str(x)
                currentstate[name] = switches[x]

def checkswitches():
        for switch in range (0,8):
                name = "switch" + str(switch)
                if ( currentstate[name] != prevstate[name]):
                        print name + " value changed to " + str(switch)


initswitches()

for y in range (0,2):

        setswitches()
        print "Loop" + str(y)
        print "Switches:"
        print switches
        print "Current state:"
        print currentstate
        print "Previous state:"
        print prevstate


        checkswitches()

        prevstate = currentstate
        switches='01001001'
        print
        print

and here is the conclusion:

Loop0
Switches:
11011101
Current state:
{'switch3': '1', 'switch2': '0', 'switch1': '1', 'switch0': '1', 'switch7': '1', 'switch6': '0', 'switch5': '1', 'switch4': '1'}
Previous state:
{'switch3': '1', 'switch2': '0', 'switch1': '1', 'switch0': '1', 'switch7': '1', 'switch6': '0', 'switch5': '1', 'switch4': '1'}

Loop1
Switches:
01001001
Current state:
{'switch3': '0', 'switch2': '0', 'switch1': '1', 'switch0': '0', 'switch7': '1', 'switch6': '0', 'switch5': '0', 'switch4': '1'}
Previous state:
{'switch3': '0', 'switch2': '0', 'switch1': '1', 'switch0': '0', 'switch7': '1', 'switch6': '0', 'switch5': '0', 'switch4': '1'}

, , - . Loop0 , Loop1 , Loop0. , , checkswitches. - , ?

+4
1
prevstate = currentstate

prevstate currentstate dict. prevstate , currentstate. currentstate prevstate.

, prevstate currentstate:

prevstate = currentstate.copy()

prevstate - currentstate:

prevstate.update(currentstate)

, , , ( ) .

+7

All Articles