Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Program gives error but doesn't say where the error is.
#1
Hi, i'm making an encryption algorithm but when I test it it gives me a strange error which i can't correct because it doesn't say where. I only have the encryption function for now.

The code:
"""A funciton to encrypt a given text."""


from random import randint


class w_encryption():
    """A class with a method to encrypt text and a method to decrypt text."""

    def __init__(self, text):
        """Declares the variables used within the methods."""
        self.text = text
        self.encrypted = []
        self.return_encrypted = ''

    def _encrypt_(self):
        """Encrypts a text."""
        for character in self.text:
            # Encrypts lowercase letters.
            if character == 'a':
                echar = randint(1, 50)
                self.encrypted.append(echar)
            elif character == 'b':
                echar = randint(300, 400)
                self.encrypted.append(echar)
            elif character == 'c':
                echar = randint(100, 150)
                self.encrypted.append(echar)
            elif character == 'd':
                echar = randint(51, 99)
                self.encrypted.append(echar)
            elif character == 'e':
                echar = randint(500, 550)
                self.encrypted.append(echar)
            elif character == 'f':
                echar = randint(700, 729)
                self.encrypted.append(echar)
            elif character == 'g':
                echar = randint(799, 830)
                self.encrypted.append(echar)
            elif character == 'h':
                echar = randint(180, 200)
                self.encrypted.append(echar)
            elif character == 'i':
                echar = randint(1000, 1100)
                self.encrypted.append(echar)
            elif character == 'j':
                echar = randint(1150, 1160)
                self.encrypted.append(echar)
            elif character == 'k':
                echar = randint(1212, 1221)
                self.encrypted.append(echar)
            elif character == 'l':
                echar = randint(1340, 1560)
                self.encrypted.append(echar)
            elif character == 'm':
                echar = randint(3000, 3002)
                self.encrypted.append(echar)
            elif character == 'n':
                echar = randint(4020, 4050)
                self.encrypted.append(echar)
            elif character == 'o':
                echar = randint(4130, 4340)
                self.encrypted.append(echar)
            elif character == 'p':
                echar = randint(6000, 6100)
                self.encrypted.append(echar)
            elif character == 'q':
                echar = randint(6120, 6130)
                self.encrypted.append(echar)
            elif character == 'r':
                echar = randint(8020, 8100)
                self.encrypted.append(echar)
            elif character == 's':
                echar = randint(8200, 8340)
                self.encrypted.append(echar)
            elif character == 't':
                echar = randint(9200, 9222)
                self.encrypted.append(echar)
            elif character == 'u':
                echar = randint(9223, 9300)
                self.encrypted.append(echar)
            elif character == 'v':
                echar = randint(9500, 9700)
                self.encrypted.append(echar)
            elif character == 'w':
                echar = randint(9701, 9720)
                self.encrypted.append(echar)
            elif character == 'x':
                echar = randint(9800, 9820)
                self.encrypted.append(echar)
            elif character == 'y':
                echar = randint(9888, 9900)
                self.encrypted.append(echar)
            elif character == 'z':
                echar = randint(9901, 9999)
                self.encrypted.append(echar)
            # Encrypts special characters.
            elif character == ' ':
                echar = randint(10000, 10100)
                self.encrypted.append(echar)
            elif character == '!':
                echar = randint(10150, 10300)
                self.encrypted.append(echar)
            elif character == '"':
                echar = randint(10350, 10423)
                self.encrypted.append(echar)
            elif character == '#':
                echar = randint(10424, 10499)
                self.encrypted.append(echar)
            elif character == '$':
                echar = randint(10500, 10505)
                self.encrypted.append(echar)
            elif character == '%':
                echar = randint(10510, 10600)
                self.encrypted.append(echar)
            elif character == '&':
                echar = randint(10602, 10700)
                self.encrypted.append(echar)
            elif character == "'":
                echar = randint(10800, 10900)
                self.encrypted.append(echar)
            elif character == '(':
                echar = randint(10904, 10912)
                self.encrypted.append(echar)
            elif character == ')':
                echar = randint(10999, 11020)
                self.encrypted.append(echar)
            elif character == '*':
                echar = randint(11050, 11111)
                self.encrypted.append(echar)
            elif character == '+':
                echar = randint(11112, 11540)
                self.encrypted.append(echar)
            elif character == ',':
                echar = randint(11600, 11620)
                self.encrypted.append(echar)
            elif character == '-':
                echar = randint(11621, 11630)
                self.encrypted.append(echar)
            elif character == '.':
                echar = randint(11700, 11809)
                self.encrypted.append(echar)
            elif character == '/':
                echar = randint(11890, 11925)
                self.encrypted.append(echar)
            elif character == ':':
                echar = randint(11930, 12000)
                self.encrypted.append(echar)
            elif character == ';':
                echar = randint(12400, 12434)
                self.encrypted.append(echar)
            elif character == '<':
                echar = randint(12456, 12479)
                self.encrypted.append(echar)
            elif character == '=':
                echar = randint(12490, 12556)
                self.encrypted.append(echar)
            elif character == '>':
                echar = randint(12587, 12600)
                self.encrypted.append(echar)
            elif character == '?':
                echar = randint(12674, 12699)
                self.encrypted.append(echar)
            elif character == '@':
                echar = randint(12805, 12999)
                self.encrypted.append(echar)
            elif character == '[':
                echar = randint(13000, 13123)
                self.encrypted.append(echar)
            elif character == '':
                echar = randint(13230, 13240)
                self.encrypted.append(echar)
            elif character == ']':
                echar = randint(13260, 13300)
                self.encrypted.append(echar)
            elif character == '^':
                echar = randint(13307, 13367)
                self.encrypted.append(echar)
            elif character == '_':
                echar = randint(13400, 13500)
                self.encrypted.append(echar)
            elif character == '`':
                echar = randint(13600, 13611)
                self.encrypted.append(echar)
            elif character == '{':
                echar = randint(13705, 13709)
                self.encrypted.append(echar)
            elif character == '|':
                echar = randint(13790, 13845)
                self.encrypted.append(echar)
            elif character == '}':
                echar = randint(13869, 13934)
                self.encrypted.append(echar)
            elif character == '~':
                echar = randint(13950, 14000)
                self.encrypted.append(echar)
            # Encrypt upper-case letters.
            elif character == 'A':
                echar = randint(14005, 14103)
                self.encrypted.append(echar)
            elif character == 'B':
                echar = randint(14134, 14189)
                self.encrypted.append(echar)
            elif character == 'C':
                echar = randint(14200, 14222)
                self.encrypted.append(echar)
            elif character == 'D':
                echar = randint(14333, 14359)
                self.encrypted.append(echar)
            elif character == 'E':
                echar = randint(14364, 14400)
                self.encrypted.append(echar)
            elif character == 'F':
                echar = randint(14401, 14444)
                self.encrypted.append(echar)
            elif character == 'G':
                echar = randint(14500, 14700)
                self.encrypted.append(echar)
            elif character == 'H':
                echar = randint(14766, 14888)
                self.encrypted.append(echar)
            elif character == 'I':
                echar = randint(14900, 14950)
                self.encrypted.append(echar)
            elif character == 'J':
                echar = randint(14956, 15000)
                self.encrypted.append(echar)
            elif character == 'K':
                echar = randint(15030, 15080)
                self.encrypted.append(echar)
            elif character == 'L':
                echar = randint(15100, 15176)
                self.encrypted.append(echar)
            elif character == 'M':
                echar = randint(15200, 15209)
                self.encrypted.append(echar)
            elif character == 'N':
                echar = randint(15243, 15250)
                self.encrypted.append(echar)
            elif character == 'O':
                echar = randint(15286, 15297)
                self.encrypted.append(echar)
            elif character == 'P':
                echar = randint(15300, 15334)
                self.encrypted.append(echar)
            elif character == 'Q':
                echar = randint(15410, 15459)
                self.encrypted.append(echar)
            elif character == 'R':
                echar = randint(15479, 15500)
                self.encrypted.append(echar)
            elif character == 'S':
                echar = randint(15555, 15604)
                self.encrypted.append(echar)
            elif character == 'T':
                echar = randint(15614, 15637)
                self.encrypted.append(echar)
            elif character == 'U':
                echar = randint(15666, 15675)
                self.encrypted.append(echar)
            elif character == 'V':
                echar = randint(15700, 15734)
                self.encrypted.append(echar)
            elif character == 'W':
                echar = randint(15736, 15777)
                self.encrypted.append(echar)
            elif character == 'X':
                echar = randint(15789, 15805)
                self.encrypted.append(echar)
            elif character == 'Y':
                echar = randint(15867, 15900)
                self.encrypted.append(echar)
            elif character == 'Z':
                echar = randint(15937, 16000)
                self.encrypted.append(echar)
            else:
                character = echar
                self.encrypted.append(echar)

        for object in self.encrypted:
            self.return_encrypted += str(object)

        return self.return_encrypted

The error:
Error:
<__main__.w_encryption object at 0x7f6e7a80be80>
Thanks in advance! :D.
Quote
#2
You are going to have to be more specific about how you are testing it. It works fine when I run it. That doesn't look like an error, it looks like the repr for an instance of w_encryption. My guess is your test is returning the encryption object when it should be returning the encrypted text.

Edit: for example:

Output:
>>> test = w_encryption('Avalokiteshvara') >>> test._encrypt_() '14061962245146643241220108292105138248193963515807825' >>> test <__main__.w_encryption instance at 0x0000000003988548>
FWendeburg likes this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
You can save yourself a whole lot of lines of code by using a dictionary
encr_dict={'a':(1, 50)
           'b':(300, 400)}
if echar in encr_dict:
    echar = randint(encr_dict(echar))
    self.encrypted.append(echar)


## etc
## replaces 
            if character == 'a':
                echar = randint(1, 50)
                self.encrypted.append(echar)
            elif character == 'b':
                echar = randint(300, 400)
                self.encrypted.append(echar)  
Finally, how are you going to decode it since you don't know which random number was used to encode it?
FWendeburg and ichabod801 like this post
Quote
#4
(Mar-08-2019, 09:21 PM)ichabod801 Wrote: You are going to have to be more specific about how you are testing it. It works fine when I run it. That doesn't look like an error, it looks like the repr for an instance of w_encryption. My guess is your test is returning the encryption object when it should be returning the encrypted text.

Edit: for example:

Output:
>>> test = w_encryption('Avalokiteshvara') >>> test._encrypt_() '14061962245146643241220108292105138248193963515807825' >>> test <__main__.w_encryption instance at 0x0000000003988548>

Thanks for your response!

I was doing:
test = w_encryption('hello')
test._encrypt_()
print(test)
Haha that's why I got "<__main__.w_encryption instance at 0x0000000003988548>" in response.

(Mar-09-2019, 01:22 AM)woooee Wrote: You can save yourself a whole lot of lines of code by using a dictionary
encr_dict={'a':(1, 50)
           'b':(300, 400)}
if echar in encr_dict:
    echar = randint(encr_dict(echar))
    self.encrypted.append(echar)


## etc
## replaces 
            if character == 'a':
                echar = randint(1, 50)
                self.encrypted.append(echar)
            elif character == 'b':
                echar = randint(300, 400)
                self.encrypted.append(echar)  
Finally, how are you going to decode it since you don't know which random number was used to encode it?

Thanks for your response!
But I already wrote the code for 80+ characters. Haha
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  ERROR:haproxystats:Error fetching stats deepakkr3110 1 64 Nov-12-2019, 12:29 PM
Last Post: Larz60+
  Error :unable to detect undefined names created in spyder ide error at line 2 milind_eac 2 341 Jul-30-2019, 10:29 PM
Last Post: milind_eac
  error message on program launch DJPY1175 0 279 Jun-10-2019, 08:40 AM
Last Post: DJPY1175
  Getting error "Type error-a bytes-like object..." mrapple2020 1 1,314 Apr-06-2019, 06:37 PM
Last Post: mrapple2020
  I have Traceback error and a type error and i dont know how to fix it coltron1282 2 309 Feb-07-2019, 03:15 PM
Last Post: coltron1282
  Attribute error print statement error jamshaid1997 1 363 Jan-20-2019, 04:02 PM
Last Post: ichabod801
  an input error or elif error , how can i fix this the_fire_pharaoh 1 400 Dec-15-2018, 09:47 PM
Last Post: Gribouillis
  Error ' Attribute error: 'str' object has no attribute 'items'' in pyinstaller manhnt 2 3,469 Nov-04-2018, 05:51 PM
Last Post: Larz60+
  Nead help with my calculator program- getting syntax error FantasyCookie17 2 535 Aug-29-2018, 04:25 AM
Last Post: FantasyCookie17
  Small sqlite3 program error pythonNoob 5 727 May-10-2018, 05:45 PM
Last Post: pythonNoob

Forum Jump:


Users browsing this thread: 1 Guest(s)