Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My first class.
#1
This code was developed after completing an intermediate challenge that suggested I write my own version of the python upper() function. After finishing I wondered what would happen if I forced all the ascii values forward by a set number. Two days later I'd come up with this. I'm not actually looking to create encryption. This was an interesting way to take a challenge I enjoyed to the next level. Was a class/object the right way to write this? I was watching youtube videos at the time and wanted to apply what I'd learned.
import random

class Encryption():
    def __init__(self, list):
        self.list = list

    def enc_inpt(self, list):
        self.list = ([*list]) # break the list into individual characters
        encryption_key = random.randrange(1, 5) # create the encryption key

        for i in range(len(self.list)): 
            starting_letter = ord(self.list[i]) # carrier variable to modify the item
            loop_count = starting_letter + encryption_key # while loop stop

            if starting_letter == 32: # check for spaces here replace with @ 
                self.list[i] = "@"

            else:
                while starting_letter <= loop_count:  
                    starting_letter += starting_letter 
                self.list[i] = chr(starting_letter) # replace the new value in the list

        self.list.insert(0, str(encryption_key)) # insert the key at the front of the string for decryption later
        return "".join(self.list)


new_list1 = Encryption([])
print(new_list1.enc_inpt("Encrypt This Sentence!"))
Thanks for looking. I'd be very great for any and all feedback. How can I do better, faster, more efficiently? I've been teaching myself python since Christmas and don't have a lot of coding experience. One of my biggest struggles is coming up with an effective way to check it, to the best of my knowledge it is working. No suggestion is to small. I'm still very new.
Reply
#2
I have doubt that the code as provided can run. On row #16:

self.list[i] = 
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Oops. Thank you for pointing that out.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020