By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,173 Members | 796 Online
Bytes IT Community
+ Ask a Question
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
  1. class Encrypt(object):
  2.  #The worst object ever created
  3.     def __init__(self,alphabet):
  4.         self.alphabet = alphabet
  5.         self.trans = self.maketrans()
  6.         self.count = -1
  7.         self.translist = []
  8.  
  9.     def maketrans(self):
  10.         x = (random.randrange(0,len(self.alphabet)))
  11.         if(self.count == len(self.alphabet)):
  12.             return(self.translist)
  13.         elif(x in self.translist):
  14.             x = input("enter a number, bitch")
  15.             self.count += 1
  16.             self.translist.append(x)
  17.             return(maketrans(self))
  18.         else:
  19.             self.count += 1
  20.             self.translist.append(x)
  21.             return(maketrans(self))
  22.  
  23. 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']
  24. encrypt1 = Encrypt(alphabet)
  25. 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 <module>
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
Share this Question
Share on Google+
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
  1. class Encrypt(object):
  2.  #The worst object ever created
  3.     def __init__(self,alphabet):
  4.         self.alphabet = alphabet
  5.         self.trans = self.maketrans()
  6.  
  7.     def maketrans(self,x,translist):
  8.         r = (random.randrange(0,len(self.alphabet)))
  9.         if(x == len(self.alphabet)):
  10.             return(translist)
  11.         elif(r in translist):
  12.             r = input("enter a number, bitch")
  13.             translist.append(x)
  14.             return(self.maketrans(self,x + 1,translist))
  15.         else:
  16.             self.translist.append(x)
  17.             return(maketrans(self,x + 1,translist))
  18.  
  19. 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']
  20. encrypt1 = Encrypt(alphabet,0,[])
  21. print(encrypt1.maketrans)
  22.  
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
  1.         self.trans = self.maketrans()
  2.         self.count = -1
Oh well, you learned how to use code tags at least.
Oct 31 '11 #4

bvdet
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

Post your reply

Sign in to post your reply or Sign up for a free account.