440,173 Members | 796 Online Need help? Post your question and get tips & solutions from a community of 440,173 IT Pros & Developers. It's quick & easy.

# Error message...object cant find attribute

 P: 2 Expand|Select|Wrap|Line Numbers class Encrypt(object):  #The worst object ever created     def __init__(self,alphabet):         self.alphabet = alphabet         self.trans = self.maketrans()         self.count = -1         self.translist = []       def maketrans(self):         x = (random.randrange(0,len(self.alphabet)))         if(self.count == len(self.alphabet)):             return(self.translist)         elif(x in self.translist):             x = input("enter a number, bitch")             self.count += 1             self.translist.append(x)             return(maketrans(self))         else:             self.count += 1             self.translist.append(x)             return(maketrans(self))   alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] encrypt1 = Encrypt(alphabet) print(encrypt.trans) ---------------------------- I get this error ----------------------- Traceback (most recent call last): File "C:\Users\iStrawser\UD\CISC 106\Pythons\lab7.py", line 76, in encrypt1 = Encrypt(alphabet) File "C:\Users\iStrawser\UD\CISC 106\Pythons\lab7.py", line 56, in __init__ self.trans = self.maketrans() File "C:\Users\iStrawser\UD\CISC 106\Pythons\lab7.py", line 62, in maketrans self.count += 1 AttributeError: 'Encrypt' object has no attribute 'count' -------------------- Why can't I get a list of numbers that encode the alphabet? I'm not sure I know what to do to make a recursive function in a class. Oct 31 '11 #1
4 Replies

 Expert 100+ P: 621 That means that it is not a member of the class, which is usually an indentation problem, but there is no way to tell since you did not hit the code tag (# at the top of the reply box) and post your code between the [code] and [/code] tags. I'm not sure I know what to do to make a recursive function in a class You would not use recursion here, just a loop for letter in alphabet: Look at the problem again and state it more clearly. Oct 31 '11 #2

 P: 2 # Expand|Select|Wrap|Line Numbers class Encrypt(object):  #The worst object ever created     def __init__(self,alphabet):         self.alphabet = alphabet         self.trans = self.maketrans()       def maketrans(self,x,translist):         r = (random.randrange(0,len(self.alphabet)))         if(x == len(self.alphabet)):             return(translist)         elif(r in translist):             r = input("enter a number, bitch")             translist.append(x)             return(self.maketrans(self,x + 1,translist))         else:             self.translist.append(x)             return(maketrans(self,x + 1,translist))   alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] encrypt1 = Encrypt(alphabet,0,[]) print(encrypt1.maketrans)   Oct 31 '11 #3

 Expert 100+ P: 621 My mistake, self.count is declared after self.maketrans() is called, which uses self.count, so just reverse these two statements in __init__. Expand|Select|Wrap|Line Numbers         self.trans = self.maketrans()         self.count = -1 Oh well, you learned how to use code tags at least. Oct 31 '11 #4

 Expert Mod 2.5K+ P: 2,851 I saw a few more problems: To call method maketrans: `self.maketrans(x,translist)` The number of arguments in Encrypt._init__() do not match the call `encrypt1 = Encrypt(alphabet,0,[])` 'Encrypt' object has no attribute 'translist' Oct 31 '11 #5 