어느부분이 틀렸는지 모르겠고 어디를 고쳐야할지 감이 안잡힙니다
조회수 1036회
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):
super(self.__class__,self).__init__(n)
self.pinA = None
self.pinB = None
self.pinB = None
def getPinA(self):
return input("Enter Pin A input for gate"+
self.getLabel()+">>")
def getPinB(self):
return input("Enter Pin B input for gate"+
self.getLabel()+">>")
def getPinC(self):
return input("Enter Pin C input for gate"+
self.getLabel()+">>")
class UnaryGate(LogicGate):
def __init__(self,n):
super(self.__class__,self).__init__(n)
self.pin=None
def getPin(self):
return input("Enter Pin input for gate"+
self.getLabel()+">>")
class AndGate(BinaryGate):
def __init__(self,n):
super(self.__class__,self).__init__(n)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
c = self.getPinC()
if a==0:
if b==0:
return 0
else: #a==0,b==1
if c==0:
return 0
else: #a==0,b==1,c==1
return 1
else: #a==1
if b==0:
if c==0:
return 0
else: #c==1
return 1
else: #a==1,b==1
if c==0:
return 1
else: #a==1,b==1,c==1
return 1
class OrGate(BinaryGate):
def __init__(self,n):
super(self.__class__,self).__init__(n)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
c = self.getPinC()
if a==0:
if b==0:
if c==0:
return 0
else: #c==1
return 1
else: #a==1
return 1
class NotGate(BinaryGate):
def __init__(self,n):
super(self.__class__,self).__init__(n)
def performGateLogic(self):
a = not(self.getPinA())
b = not(self.getPinB())
c = not(self.getPinC())
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 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")
def getPinA(self):
if self.PinA == None:
return input("Enter Pin A input for gate"+
self.getName()+">>")
else:
return self.pinA.getFrom().getOutput()
def getPinB(self):
if self.pinB == None:
return input("Enter Pin B input for gate"+
self.getName()+">>")
else:
return self.pinB.getFrom().getOutput()
def getPinC(self):
if self.pinC == None:
return input("Enter Pin C input for gate"+
self.getName()+">>")
else:
return self.pinC.getFrom().getOutput()
def main():
'''Construct and evaluate XOR gates with And, Or, and Not gates'''
X = getPinA("X")
Y = getPinB("Y")
Z = getPinC("Z")
N1 = NotGate("NOT1")
N2 = NotGate("NOT2")
N3 = NotGate("NOT3")
A1 = AndGate("AND1")
A2 = AndGate("AND2")
A3 = AndGate("AND3")
A4 = AndGate("AND4")
A5 = AndGate("And5")
A6 = AndGate("And6")
A7 = AndGate("And7")
A8 = AndGate("And8")
O1 = OrGate("OR1")
O2 = OrGate("OR2")
O3 = OrGate("OR3")
O4 = OrGate("OR4")
O5 = OrGate("OR5")
X_N1 = Connector(X, N1)
X_A8 = Connector(X, A8)
X_A5 = Connector(X, A5)
X_A6 = Connector(X, A6)
Y_N2 = Connector(Y, N2)
Y_A2 = Connector(Y, A2)
Y_A4 = Connector(Y, A4)
Y_A5 = Connector(Y, A5)
Z_A1 = Connector(Z, A1)
Z_N3 = Connector(Z, N3)
Z_A4 = Connector(Z, A4)
Z_A6 = Connector(Z, A6)
N1_A7 = Connector(N1, A7)
N2_A1 = Connector(N2, A1)
N2_A3 = Connector(N2, A3)
N3_A2 = Connector(N3, A2)
N3_A3 = Connector(N3, A3)
A1_O1 = Connector(A1, O1)
A2_O1 = Connector(A2, O1)
A3_O2 = Connector(A3, O2)
A4_O2 = Connector(A4, O2)
A4_O3 = Connector(A4, O3)
A5_O3 = Connector(A5, O3)
A6_O4 = Connector(A6, O4)
O1_A7 = Connector(O1, A7)
O2_A8 = Connector(O2, A8)
O3_O4 = Connector(O3, O4)
A7_O5 = Connector(A7, O5)
A8_O5 = Connector(A8, O5)
print("Output S: ", O5.getOutput())
print("Output C: ", O4.getOutput())
print("total :", O4.getOutput(), O5.getOutput())
if __name__ == "__main__":
main()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-35-c9626ee0b9f4> in <module>()
68
69 if __name__ == "__main__":
---> 70 main()
<ipython-input-35-c9626ee0b9f4> in main()
3 def main():
4 '''Construct and evaluate XOR gates with And, Or, and Not gates'''
----> 5 X = getPinA("X")
6 Y = getPinB("Y")
7 Z = getPinC("Z")
<ipython-input-30-60f20c8f6e74> in getPinA(self)
9
10 def getPinA(self):
---> 11 if self.PinA == None:
12 return input("Enter Pin A input for gate"+
13 self.getName()+">>")
AttributeError: 'str' object has no attribute 'PinA'
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
이 코드엔 2가지 문제가 있네요.
main() 5번째 라인을 보면
X = getPinA("X")
를 호출하고 있네요.하지만
getPinA()
메소드는 최상 위에 정의된 것이 없네요.BinaryGate 클래스와 Connector 클래스 밑에 정의되긴 하였지만, main() 에서 호출하면 클래스 밑에 정의된 메소드를 찾는 것이 아니라 클래스 밖에 정의된 메소드를 찾게 됩니다.
클래스 밑에 정의된 메소드를 호출하는 경우 해당 클래스의 인스턴스를 생성한 뒤 그 인스턴스의 메소드를 호출해야 합니다.
Connector con = Connector() con.getPinA()
이런 형태로 작성해야되는거죠.
또 메인함수에서 getPinA() 메소드를 호출 할 때, "X"를 매개변수로 넘기는 것으로 보이는데요.
정의된 getPinA() 메소드 어디에서도 String형태의 변수를 매개변수로 받지를 않네요.
확실한건 python의 클래스와 객체에 대한 학습이 필요하신 것 같습니다.
def getPinA(self)
에서self
가 무엇인지 이해하실 때 쯤 위 코드의 문제가 보일 듯 싶네요.파이썬 강의를 해야할 정도라 여기 답변만으로는 불가능할 것 같습니다.
댓글 입력