Jan-09-2023, 12:50 AM
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.