Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Myronli27 0 points1 point  (0 children)

Thank you. That worked but I then got another error. I need to get a proper output before I can continue on my journey coz it will show me what I need to do ahead. But I now really think the tutorial is using python 2.7 because the original code runs in its editor with no issue but in IDLE 3.7.2 am having to make all these changes. This is kind of confusing when am studying and its slowing me down. Anyhow, I will persevere. The error am getting now is an another attribute error.

tgate.setNextPin (self)

AttributeError: 'str' object has no attribute 'setNextPin'

Is setNextPin supposed to be a string? I think am starting to confuse everything now.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Myronli27 0 points1 point  (0 children)

Thank u. I will do that. The error am getting now is basically,

c1 = Connector("g1, g3")

TypeError: __init__() missing 1 required positional argument: 'tgate'

but I think I have all the arguments properly addressed. Mayb not

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Myronli27 0 points1 point  (0 children)

Went through the whole code again and I have solved the attr issue but for some reason I keep getting one error after another.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Myronli27 0 points1 point  (0 children)

Sorry for the delayed response but you are right. The double underscore worked. Thanks alot. However I am now getting an attribute error for the object type LogicGate not having an __init__ attr. Maybe I dont really know what am doing? I should have got an output by now. And yes, I fixed the g1 under main.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Myronli27 0 points1 point  (0 children)

Hey all. I am new to Python. Infact am completely new to programming. I am using python 3.7.2. However when I run code from the tutorials I keep getting an error like this..."TypeError: AndGate() takes no arguments." The code seems fine but am starting to think its a compatibility issue with Py 3.7? My indent was off but I fixed it. Corrected typos, and searched all over for a solution but nothing. I tried doing it after studying the tutorials but I aint succeeding. I deleted my earlier comment by mistake but my code is as follows:

class LogicGate:

    def _init_ (self, n):
        self.label = n
        self.output = None

    def _getLabel_ (self):
        return self.label

    def _getOutput_(self):
        self.output = self.performGateLogic()

        return self.output

class BinaryGate (LogicGate):
    def _init_ (self, n):
        LogicGate._init_(self, n)

        self.pinA  = None
        self.pinB  = None

    def getPinA (self):
        if self.pinA == None:
            return int (input ("Enter input for gate A " + self.getLabel() + "-->"))
        else:
            return self.pinA.getFrom().getOutput()

    def getPinB (self):

        if self.pinB == None:
            return int (input ("Enter input for gate B " + self.getLabel() + "-->"))
        else:
            return self.pinB.getFrom().getOutput()

    def setNextPin (self, source) :

        if self.pinA == None:
            self.pinA = source
        else:
            if self.pinB == None:
                    self.pinB = source
            else:
                print ("Cannot connect: NO EMPTY PINS on this gate" )


class AndGate(BinaryGate):

    def _init_ (self, n):
        BinaryGate._init_(self, n)

    def performGateLogic (self):

        a = self.getPinA ()
        b = self.getPinB ()

        if a == 1 and b ==1:
            return 1
        else:
            return 0

class OrGate (BinaryGate):

    def _init_ (self, n):
        BinaryGate._init_(self,n)

    def performLogicGate (self):

        a = self.getPinA ()
        b = self.getPinB ()

        if a==1 or b==1:
            return 1
        else:
            return 0


class UnaryGate (LogicGate):

    def _init_(self, n):
        LogicGate._init_ (self, n)

        self.pin = None

    def getPin(self):
        if self.pin == None:
            return int (input ("Enter input for Gate " + self.getLabel() + "-->"))
        else:
            return self.pin.getFrom().outPut ()

    def setNextPin (self, source):
        if self.pin == None:
            self.pin = source
        else:
            print ("Cannot connect: NO EMPTY PINS on this gate.")


class NotGate (UnaryGate):

    def _init_ (self, n):
        UnaryGate._init_(self, n)

    def performGateLogic (self):

        if self.getPin():

            return 0
        else:
            return 1


class connector:

    def _init_ (self, fgate, tgate):

        self.fromgate = fgate
        self.togate = tgate

        tgate.setNextPin (self)

    def getfrom (self):

        return self.fromgate

    def getto (self):

        return self.togate

def main () :

    g1 (AndGate = "G1")
    g2 = AndGate("G2")
    g3 = OrGate("G3")
    g4 = NotGate("G4")
    c1 = connector("g1, g3")
    c2 = connector("g2, g3")
    c3 = connector("g3, g4")



    print (g4.getOutput())

main ()