AttributeError: 'Rsa' object has no attribute 'n' - python

I am getting this error:
Traceback (most recent call last):
File "/Users/aditya/Desktop/Pycharm /Fire_Tech_Camp/Rsa/RsaMainFrame.py", line 7, in <module>
rsa = RsaEncryptionAndDecryption.Rsa()
File "/Users/aditya/Desktop/Pycharm /Fire_Tech_Camp/Rsa/RsaEncryptionAndDecryption.py", line 8, in __init__
self.p, self.q = self.findingPandQ()
File "/Users/aditya/Desktop/Pycharm /Fire_Tech_Camp/Rsa/RsaEncryptionAndDecryption.py", line 80, in findingPandQ
while not self.isPrime(self.a):
File "/Users/aditya/Desktop/Pycharm /Fire_Tech_Camp/Rsa/RsaEncryptionAndDecryption.py", line 14, in isPrime
for i in range(2,int(self.n**0.5)+1):
AttributeError: 'Rsa' object has no attribute 'n'
What I am trying to do is create a class with a function (isPrime()) to check primes.
Then in a seperate function I check if it is prime (generate_keys()). The class is being called in a seperate file.
Code:
Main File where I call the class:
from Rsa import RsaEncryptionAndDecryption
from appJar import gui
app = gui()
rsa = RsaEncryptionAndDecryption.Rsa()
def encode(name):
msg = app.getEntry('Input to Encode Here')
if msg != '':
p, q = rsa.findingPandQ()
while p == q:
p, q = rsa.findingPandQ()
n, e, d = rsa.generate_keys(p, q)
print(n, e, d)
I run this function in this
app.addButton('Encode', encode, 3, 3)
The class code is here:
import random
from math import gcd
class Rsa:
def __init__(self):
self.p, self.q = self.findingPandQ()
self.n = self.p * self.q
self.phi = (self.p - 1) * (self.q - 1)
self.e = 0
def isPrime(self, n):
for i in range(2,int(self.n**0.5)+1):
if self.n%i == 0:
return False
return True
def modReverse(self, phi, e):
self.pos00 = self.phi
self.pos01 = self.phi
self.pos10 = self.e
self.pos11 = 1
self.newpos10 = 0
while self.newpos10 != 1:
self.pos00pos10int = self.pos00 // self.pos10
self.inttimespos10 = self.pos00pos10int * self.pos10
self.inttimespos11 = self.pos00pos10int * self.pos11
self.newpos10 = self.pos00 - self.inttimespos10
if self.newpos10 < 0:
self.newpos10 %= phi
self.newpos11 = self.pos01 - self.inttimespos11
if self.newpos11 < 0:
self.newpos11 %= phi
self.pos00 = self.pos10
self.pos01 = self.pos11
self.pos10 = self.newpos10
self.pos11 = self.newpos11
return self.newpos11
def coprime(self, a, b):
return gcd(self.a, self.b) == 1
def findingPandQ(self):
self.a = random.randint(10,100)
while not self.isPrime(self.a):
self.a = random.randint(10,100)
self.b = random.randint(10,100)
while not self.isPrime(self.b):
self.b = random.randint(10,100)
return self.a, self.b
def generate_keys(self, p, q):
for i in range(self.phi):
if self.isPrime(i):
if self.coprime(i, self.phi):
self.e = i
self.d = self.modReverse(self.phi, self.e)
while self.e == self.d:
self.p, self.q = self.findingPandQ(self.p, self.q)
while self.p == self.q:
self.p, self.q = self.findingPandQ(self.p, self.q)
self.n, self.e, self.d = self.generate_keys(self.p, self.q)
return self.n, self.e, self.d

You are calling your functions before you have set self.n:
def __init__(self):
self.p, self.q = self.findingPandQ()
self.n = self.p * self.q
self.n is set only after you called findingPandQ(). Because you call isPrime() from findingPandQ(), and isPrime() assumes that self.n exists, that fails.
You probably made a simple error; you should be using n (the argument passed to isPrime(), and not self.n:
def isPrime(self, n):
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True

Related

pso algorithm on test function. But I'm stuck with python code

So I have to classes Swarm and Particles, and a class for Sphere function.
But when I'm trying to run the optimization, I get error on iteretation and I don't understand why:
TypeError: '<' not supported between instances of 'NoneType' and 'NoneType'
Eventually, if you can give me some tipf for shifted sphere function.
from abc import ABCMeta, abstractmethod
import numpy
import numpy.random
from particle import Particle
class Swarm (object):
__metaclass__ = ABCMeta
def __init__ (self,
swarmsize,
minvalues,
maxvalues,
currentVelocityRatio,
localVelocityRatio,
globalVelocityRatio):
self.__swarmsize = swarmsize
assert len (minvalues) == len (maxvalues)
assert (localVelocityRatio + globalVelocityRatio) > 4
self.__minvalues = numpy.array (minvalues[:])
self.__maxvalues = numpy.array (maxvalues[:])
self.__currentVelocityRatio = currentVelocityRatio
self.__localVelocityRatio = localVelocityRatio
self.__globalVelocityRatio = globalVelocityRatio
self.__globalBestFinalFunc = None
self.__globalBestPosition = None
self.__swarm = self.__createSwarm ()
def __getitem__ (self, index):
"""
Возвращает частицу с заданным номером
"""
return self.__swarm[index]
def __createSwarm (self):
return [Particle (self) for _ in range (self.__swarmsize) ]
def nextIteration (self):
for particle in self.__swarm:
return particle.nextIteration (self)
#property
def minvalues (self):
return self.__minvalues
#property
def maxvalues (self):
return self.__maxvalues
#property
def currentVelocityRatio (self):
return self.__currentVelocityRatio
#property
def localVelocityRatio (self):
return self.__localVelocityRatio
#property
def globalVelocityRatio (self):
return self.__globalVelocityRatio
#property
def globalBestPosition (self):
return self.__globalBestPosition
#property
def globalBestFinalFunc (self):
return self.__globalBestFinalFunc
def getFinalFunc (self, position):
assert len (position) == len (self.minvalues)
finalFunc = self._finalFunc (position)
if (self.__globalBestFinalFunc == None or
finalFunc < self.__globalBestFinalFunc):
self.__globalBestFinalFunc = finalFunc
self.__globalBestPosition = position[:]
#abstractmethod
def _finalFunc (self, position):
pass
#property
def dimension (self):
return len (self.minvalues)
def _getPenalty (self, position, ratio):
penalty1 = sum ([ratio * abs (coord - minval)
for coord, minval in zip (position, self.minvalues)
if coord < minval ] )
penalty2 = sum ([ratio * abs (coord - maxval)
for coord, maxval in zip (position, self.maxvalues)
if coord > maxval ] )
return penalty1 + penalty2
class Particle (object):
def __init__ (self, swarm):
self.__currentPosition = self.__getInitPosition (swarm)
self.__localBestPosition = self.__currentPosition[:]
self.__localBestFinalFunc = swarm.getFinalFunc (self.__currentPosition)
self.__velocity = self.__getInitVelocity (swarm)
#property
def position (self):
return self.__currentPosition
#property
def velocity (self):
return self.__velocity
def __getInitPosition (self, swarm):
return numpy.random.rand (swarm.dimension) * (swarm.maxvalues - swarm.minvalues) + swarm.minvalues
def __getInitVelocity (self, swarm):
assert len (swarm.minvalues) == len (self.__currentPosition)
assert len (swarm.maxvalues) == len (self.__currentPosition)
minval = -(swarm.maxvalues - swarm.minvalues)
maxval = (swarm.maxvalues - swarm.minvalues)
return numpy.random.rand (swarm.dimension) * (maxval - minval) + minval
def nextIteration (self, swarm):
rnd_currentBestPosition = numpy.random.rand (swarm.dimension)
rnd_globalBestPosition = numpy.random.rand (swarm.dimension)
veloRatio = swarm.localVelocityRatio + swarm.globalVelocityRatio
commonRatio = (2.0 * swarm.currentVelocityRatio /
(numpy.abs (2.0 - veloRatio - numpy.sqrt (veloRatio ** 2 - 4.0 * veloRatio) ) ) )
newVelocity_part1 = commonRatio * self.__velocity
newVelocity_part2 = (commonRatio *
swarm.localVelocityRatio *
rnd_currentBestPosition *
(self.__localBestPosition - self.__currentPosition) )
newVelocity_part3 = (commonRatio *
swarm.globalVelocityRatio *
rnd_globalBestPosition *
(swarm.globalBestPosition - self.__currentPosition) )
self.__velocity = newVelocity_part1 + newVelocity_part2 + newVelocity_part3
self.__currentPosition += self.__velocity
finalFunc = swarm.getFinalFunc (self.__currentPosition)
if finalFunc < self.__localBestFinalFunc:
self.__localBestPosition = self.__currentPosition[:]
self.__localBestFinalFunc = finalFunc
class Swarm_X2 (Swarm):
def init (self,
swarmsize,
minvalues,
maxvalues,
currentVelocityRatio,
localVelocityRatio,
globalVelocityRatio):
Swarm.init (self,
swarmsize,
minvalues,
maxvalues,
currentVelocityRatio,
localVelocityRatio,
globalVelocityRatio)
def _finalFunc (self, position):
penalty = self._getPenalty (position, 10000.0)
finalfunc = sum (position * position)
return finalfunc + penalty
from swarm_x2 import Swarm_X2
from utils import printResult
if name == "main":
iterCount = 300
dimension = 5
swarmsize = 200
minvalues = numpy.array ([-100] * dimension)
maxvalues = numpy.array ([100] * dimension)
currentVelocityRatio = 0.1
localVelocityRatio = 1.0
globalVelocityRatio = 5.0
swarm = Swarm_X2 (swarmsize,
minvalues,
maxvalues,
currentVelocityRatio,
localVelocityRatio,
globalVelocityRatio
)
for n in range (iterCount):
print "Position", swarm[0].position
print "Velocity", swarm[0].velocity
print printResult (swarm, n)
swarm.nextIteration()

How to add a function to take data from a .txt list?

I need to change the code in line # 39
I have approximately seen scripts that use with open ("file.txt", "r") as f: take data from a text document.
I have a list of "Point.txt"
g = Point(250,127)
g = Point(330,224)
g = Point(557,186)
g = Point(370,197)
g = Point(222,107)
Need to add a function so that the script takes data from the list of the document "Point.txt"
and the whole result was saved in one document "Save.txt"
class Point(object):
def __init__(self, _x, _y, _order = None): self.x, self.y, self.order = _x, _y, _order
def calc(self, top, bottom, other_x):
l = (top * inverse_mod(bottom)) % p
x3 = (l * l - self.x - other_x) % p
return Point(x3, (l * (self.x - x3) - self.y) % p)
def double(self):
if self == INFINITY: return INFINITY
return self.calc(3 * self.x * self.x, 2 * self.y, self.x)
def __add__(self, other):
if other == INFINITY: return self
if self == INFINITY: return other
if self.x == other.x:
if (self.y + other.y) % p == 0: return INFINITY
return self.double()
return self.calc(other.y - self.y, other.x - self.x, other.x)
def __mul__(self, e):
if self.order: e %= self.order
if e == 0 or self == INFINITY: return INFINITY
result, q = INFINITY, self
while e:
if e&1: result += q
e, q = e >> 1, q.double()
return result
def __str__(self):
if self == INFINITY: return "infinity"
return " %x %x" % (self.x, self.y)
def inverse_mod(a):
if a < 0 or a >= p: a = a % p
c, d, uc, vc, ud, vd = a, p, 1, 0, 0, 1
while c:
q, c, d = divmod(d, c) + (c,)
uc, vc, ud, vd = ud - q*uc, vd - q*vc, uc, vc
if ud > 0: return ud
return ud + p
p, INFINITY = 1693, Point(None, None)
g = Point(250,127)
wave = 78
result = ' ID: %x\n getID: %s' % (wave, g*wave)
f = open('Save.txt', 'a')
f.write(result)
f.close()
I have used regex to extract the parameters that you have to pass for Point creation :
import re
f = open('Save.txt', 'a')
with open('Point.txt', 'rb') as points_txt_file:
for line in points_txt_file:
found_points = re.search(r'Point\((\s*\d+\s*),(\s*\d+\s*)\)', f'{line}')
print(found_points.groups())
param1 = int(found_points.group(1))
param2 = int(found_points.group(2))
g = Point(param1, param2)
result = ' ID: %x\n getID: %s' % (wave, g*wave)
f.write(result)
f.close()
Remove your code from line #38 and use this code .

Defining a function within a class in Python 2.7

I tried to add a pgdc function in the Fraction class in order to calculate the biggest common divided or a number pgdc ... The function should be recursive. However, I keep getting the following error:
Traceback (most recent call last):
File "C:/Users/jf/Desktop/Python-jEdit_v2/23_fraction.py", line 60, in <module>
p = a.plus(b)
File "C:/Users/jf/Desktop/Python-jEdit_v2/23_fraction.py", line 35, in plus
return resultat.simplifier()
File "C:/Users/jf/Desktop/Python-jEdit_v2/23_fraction.py", line 27, in simplifier
p = pgcd(self.num, self.den)
NameError: global name 'pgcd' is not defined
In addition,
I also got the following error for the simplifier : AttributeError: Fraction instance has no attribute 'simplifier'
Which, leave me puzzle, as I tried to add self.pgcd=pgcd and self.simplifier=simplifier
at various places and it still did not work well...
# -*- coding: utf-8 -*-
class Fraction():
# constructeur et attributs
def __init__(self, num = 0, den = 1):
self.num = num
if (den == 0):
erreur("Dénominateur nul") #fonction d'erreur
self.den = den
def __str__(self):
return str(self.num) + "/" + str(self.den)
def erreur(message):
print "Erreur: " + message
from sys import exit
exit()
def pgcd(a, b):
if a == b:
return a
if a > b:
return pgcd(a-b, b)
else:
return pgcd(a, b-a)
def simplifier(self):
p = pgcd(self.num, self.den)
self.num = self.num / p
self.den = self.den / p
return self
def plus(self, f):
resultat = Fraction(self.num * f.den + self.den * f.num, \
self.den * f.den)
return resultat.simplifier()
def moins(self, f):
resultat = Fraction(self.num * f.den - self.den * f.num, \
self.den * f.den)
if (resultat.num < 0):
# changer de signe avant de simplifier
resultat.num = - resultat.num
resultat = resultat.simplifier()
resultat.num = - resultat.num
return resultat
else:
return resultat.simplifier()
def fois(self, f):
resultat = Fraction(self.num * f.num, self.den * f.den)
return resultat.simplifier()
def div(self, f):
resultat = Fraction(self.num * f.den, self.den * f.num)
return resultat.simplifier()
#### CONSOLE ####
a = Fraction(5, 11)
b = Fraction(3, 7)
p = a.plus(b)
You need to declare self as a parameter of your function and use it when you want to recursively call pgcd:
def pgcd(self,a, b):
if a == b:
return a
if a > b:
return self.pgcd(a-b, b)
else:
return self.pgcd(a, b-a)
Also, call pgcd with self. on this line:
p = self.pgcd(self.num, self.den) #from pgcd(self.num, self.den)

How to convert int to hex in python

I try to run spritz encryption code in python . which it have to convert in to hex. The same code is
result = self.s.int_to_hex(self.s.hash(256, self.s.int_string("ABC"), 32))
print(result)
It have no error but I want to reduce permutation array from 256 to 16. So I change the code like this.
result = self.s.int_to_hex(self.s.hash(16, self.s.int_string("ABC"), 32))
print(result)
Then it show error like this.
.E
======================================================================
ERROR: test_hash (__main__.TestSpritz)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:/Python34/test.py", line 9, in test_hash
result = self.s.int_to_hex(self.s.hash(16, self.s.int_string("ABC"), 32))
File "C:/Python34\spritz.py", line 130, in hash
self.absorb(M)
File "C:/Python34\spritz.py", line 73, in absorb
self.absorb_byte(byte)
File "C:/Python34\spritz.py", line 68, in absorb_byte
self.absorb_nibble(self.high(b))
File "C:/Python34\spritz.py", line 63, in absorb_nibble
self.S[self.a], self.S[int(math.floor(self.N/2)+x)] = self.S[int(math.floor(self.N/2)+x)], self.S[self.a]
IndexError: list index out of range
----------------------------------------------------------------------
Ran 2 tests in 0.031s
FAILED (errors=1)
I don't know how to use function int_to_hex please help me.
This is my code.
test.py
import unittest
from spritz import Spritz
class TestSpritz(unittest.TestCase):
def setUp(self):
self.s = Spritz()
def test_hash(self):
result = self.s.int_to_hex(self.s.hash(16, self.s.int_string("ABC"), 32))
print(result)
self.assertEqual(result[0:16], '028FA2B48B934A18')
result = self.s.int_to_hex(self.s.hash(256, self.s.int_string("spam"), 32))
self.assertEqual(result[0:16], 'ACBBA0813F300D3A')
result = self.s.int_to_hex(self.s.hash(256, self.s.int_string("arcfour"), 32))
self.assertEqual(result[0:16], 'FF8CF268094C87B9')
def test_encryption(self):
result = self.s.array_to_string(self.s.encrypt(self.s.int_string("ABC"), [0]*8))
self.assertEqual(result, '779A8E01F9E9CBC0')
result = self.s.array_to_string(self.s.encrypt(self.s.int_string("spam"), [0]*8))
self.assertEqual(result, 'F0609A1DF143CEBF')
result = self.s.array_to_string(self.s.encrypt(self.s.int_string("arcfour"), [0]*8))
self.assertEqual(result, '1AFA8B5EE337DBC7')
if __name__ == '__main__':
unittest.main()
spritz.py
import math
from fractions import gcd
class Spritz():
alt = False
def initialize_state(self, N_input):
self.i = 0
self.j = 0
self.k = 0
self.z = 0
self.a = 0
self.w = 1
self.N = N_input
self.D = int(math.ceil(math.sqrt(self.N)))
self.S = list(range(self.N))
def low(self, b):
return b % self.D
def high(self, b):
return int(math.floor(b/self.D))
def update(self):
self.i = (self.i + self.w) % self.N
self.j = (self.k + self.S[(self.j + self.S[self.i]) % self.N]) % self.N
self.k = (self.i + self.k + self.S[self.j]) % self.N
self.S[self.i], self.S[self.j] = self.S[self.j], self.S[self.i]
def whip(self, r):
for v in list(range(r)):
self.update()
self.w = (self.w + 1) % self.N
while gcd(self.w, self.N) != 1:
self.w = (self.w + 1) % self.N
def crush(self):
for v in list(range(int(math.floor(self.N/2)))):
if self.S[v] > self.S[self.N-1-v]:
self.S[v], self.S[self.N-1-v] = self.S[self.N-1-v], self.S[v]
def alt_crush(self):
for v in list(range(int(math.floor(self.N/2)))):
x = self.S[v]
y = self.S[N-1-v]
if x > y:
self.S[v] = y
self.S[N-1-v] = x
else:
self.S[v] = x
self.S[N-1-v] = y
def shuffle(self):
self.whip(2*self.N)
self.alt_crush() if self.alt else self.crush()
self.whip(2*self.N)
self.alt_crush() if self.alt else self.crush()
self.whip(2*self.N)
self.a = 0
def absorb_nibble(self, x):
if self.a == int(math.floor(self.N/2)):
self.shuffle()
self.S[self.a], self.S[int(math.floor(self.N/2)+x)] = self.S[int(math.floor(self.N/2)+x)], self.S[self.a]
self.a = (self.a + 1) % self.N
def absorb_byte(self, b):
self.absorb_nibble(self.low(b))
self.absorb_nibble(self.high(b))
def absorb(self, I):
for v in list(range(0, len(I), 3)):
byte = int(I[v:v+3])
self.absorb_byte(byte)
def absorb_stop(self):
if self.a == int(math.floor(self.N/2)):
self.shuffle()
self.a = (self.a + 1) % self.N
def output(self):
self.z = self.S[(self.j + self.S[(self.i + self.S[(self.z + self.k) % self.N]) % self.N]) % self.N]
return self.z
def drip(self):
if self.a > 0:
self.shuffle()
self.update()
return self.output()
def squeeze(self, r):
if self.a > 0:
self.shuffle()
P = ""
for v in list(range(r)):
#P.append("%0.2X" % self.drip())
P += "%03d" % self.drip()
return P
def print_variables(self):
print ("i: " + hex(self.i))
print ("j: " + hex(self.j))
print ("k: " + hex(self.k))
print ("z: " + hex(self.z))
print ("a: " + hex(self.a))
print ("w: " + hex(self.w))
print ("D: " + hex(self.D))
print ("S: " + self.array_to_string(self.S))
#print "S: " + str(self.S)
def int_string(self, string):
key = ""
for char in string:
key += "%03d" % ord(char)
return key
def int_to_hex(self, int_string):
result = ""
for i in list(range(0, len(int_string), 3)):
result += "%0.2X" % int(int_string[i:i+3])
return result
def array_to_string(self, array):
result = ""
for i in array:
result += "%0.2X" % i
return result
def hash(self, N, M, r):
self.initialize_state(N)
self.absorb(M)
self.absorb_stop()
self.absorb_byte(r)
return self.squeeze(r)
def key_setup(self, K):
self.initialize_state(256)
self.absorb(K)
def encrypt(self, K, M):
self.key_setup(K)
Z_string = self.squeeze(len(M))
Z = [int(Z_string[i:i+3]) for i in list(range(0, len(Z_string), 3))]
C = [i + j for i, j in zip(M, Z)]
return C

Instance has no attribute _pi

I'm trying to make a Python module like this:
class square:
def _init_(self):
self._length = 0
self._perimeter = 0
self._area = 0
def setLength(self, length):
self._length = float(length)
self._perimeter = 0
self._area = 0
def getLength(self):
return self._length
def getPerimeter(self):
if self._perimeter == 0:
self._perimeter = float(self._length * 4)
return self._perimeter
def getArea(self):
if self._area == 0:
self._area = float(self._length * self._length)
return self._area
class rectangle:
def _init_(self):
self._length = 0
self._width = 0
self._perimeter = 0
self._area = 0
def setLength(self, length):
self._length = float(length)
self._perimeter = 0
self._area = 0
def getLength(self):
return self._length
def setWidth(self, width):
self._width = float(width)
self._perimeter = 0
self._area = 0
def getWidth(self):
return self._width
def getPerimeter(self):
if self._perimeter == 0:
perim1 = float(self._length * 2)
perim2 = float(self._width * 2)
self._perimeter = float(perim1 + perim2)
return self._perimeter
def getArea(self):
if self._area == 0:
self._area = float(self._length * self._width)
return self._area
class circle:
def _init_(self):
self._radius = 0
self._diameter = 0
self._circumference = 0
self._pi = 3.14159265
def setRadius(self, radius):
self._radius = float(radius)
self._diameter = float(self._radius * 2)
self._circumference = 0
def setDiameter(self, diameter):
self._diameter = float(diameter)
self._radius = float(self._diameter / 2)
self._circumference = 0
def getRadius(self):
return self._radius
def getDiameter(self):
return self._diameter
def getPi(self):
return self._pi
def getCircumference(self):
if self._circumference == 0:
self._circumference = float(self._diameter * self._pi)
return self._circumference
class triangle:
def _init_(self):
self._side = []
self._side[0] = 0
self._side[1] = 0
self._side[2] = 0
self._side[3] = 0
self._angle = []
self._angle[0] = 0
self._angle[1] = 0
self._angle[2] = 0
self._angle[3] = 0
self._perimeter = 0
self._area = 0
def setSide(self, side, length):
self._side[side] = float(length)
def getSide(self, side):
return self._side[side]
def getPerimeter(self):
if self._perimeter == 0:
self._perimeter = side[1] + side[2] + side[3]
return self._perimeter
def setAngle(self, angle, measure):
self._angle[angle] = float(measure)
def getAngle(self, angle):
if self._angle[angle] == 0:
if angle == 1:
angle1 = self._angle[2]
angle2 = self._angle[3]
elif angle == 2:
angle1 = self._angle[1]
angle2 = self._angle[3]
elif angle == 3:
angle1 = self._angle[1]
angle2 = self._angle[2]
anglet = angle1 + angle2
angler = float(180) - anglet
self._angle[angle] = angler
return self.angle[angle]
It's part of a package named Mathworks. The calling code is this:
import mathworks as mw
mycircle = mw.shapes.circle()
mycircle.setDiameter(5)
circum = mycircle.getCircumference()
print circim
When I try two run the second module, I get this:
Traceback (most recent call last):
File "<string>", line 254, in run_nodebug
File "<module1>", line 21, in <module>
File "<module1>", line 17, in main
File "C:\Python27\lib\site-packages\mathworks\shapes.py", line 70, in getCircumference
self._circumference = float(self._diameter * self._pi)
AttributeError: circle instance has no attribute '_pi'
What's wrong? It works if I replace self._pi with 3.14159265, but I need it to work the other way.
You didn't name your initializers correctly, it needs double underscores on either end:
def __init__(self):
You need to correct that for all your classes.
Because you didn't name them correctly, they are not being run when you create an instance of your classes.
Next problem you'll run into is your triangle initializer; you cannot address indexes in an empty list. Create the whole list in one go instead:
def __init__(self):
self._side = [0, 0, 0, 0]
self._angle = [0, 0, 0, 0]
self._perimeter = 0
self._area = 0
You have the constructor written incorrectly. It should be __init__, not _init_, i.e. double underscores.
Because you wrote it wrong, it's not being called, so those attributes aren't being created when you instantiate your objects.

Categories