I am here to ask for criticism, as for my first week of Python I feel like I have improved a lot. I recreated the first program I made within my first hour with a tutorial, the one I made now is based on the knowledge I have gained within the week. It is my first language so I am sorry if the syntax is confusing
I really really want to improve and have the ability feel like home in Python. Having my imagination as limits instead of not knowing how to implement it in code.
First Project
The code is a mess here with it having
- no functions
- messy variables
- non reusable code
- running on the same file.
import random
import string
chars = " " + string.punctuation + string.digits + string.ascii_letters
chars = list(chars)
keys = chars.copy()
'''
Fully aware that I can just use random.shuffle(keys)
but I want to be able to decrypt the messages without
it randomizing each time I use it, can save in text file
but it will be inaccessible to others that don't have the
txt file.
'''
# Sets of keys that the randInt will choose from
keys1 = [';', 'S', 'p', 'Q', 'O', 'f', 'X', 'x', ')', 'Y', 'Z', 'I', 'q', '2', '(', 'e', '3', 'w', 'R', '5', 'u', 'n', '7', '#', '4', '/', 'l', '_', 'd', "'", '?', '|', ',', 'T', 'E', 'F', 'k', '\\', 'A', '}', 'j', 'r', 't', '0', '&', 'y', '%', ']', '<', 'v', 'C', 'N', '9', 'W', 'D', '$', '^', '*', 'i', '{', '-', ' ', '6', 'H', 'P', 'G', '~', 'a', ':', 'M', 's', 'J', 'z', 'm', 'U', '"', 'K', '[', 'L', 'o', '+', 'c', '@', '!', '=', '8', '.', 'V', 'h', 'b', '`', '1', 'g', 'B', '>']
keys2 = ['d', ']', '(', '.', "'", 'P', 'l', 'j', 'g', 'u', 'E', '{', '-', 'q', '~', 'o', '[', '9', 'V', ',', 'y', '\\', '1', '/', 'a', 'F', 'n', 'M', 'p', '6', 'z', 'H', 'v', '7', 'Z', 'Y', '5', 'W', '>', '`', '|', '$', '#', ':', 'i', 'X', '*', '%', 'D', '"', 'c', ' ', 'C', '^', 'k', '4', 'B', 'b', '}', 'A', 'Q', 's', 'I', ';', 'm', 'N', 'K', 'f', 'r', 'G', '0', '3', '!', 'J', '<', '8', ')', 'R', 'L', '=', '_', 'S', '&', 't', 'U', 'h', '+', 'w', '2', 'x', 'O', 'T', 'e', '?', '@']
# Random integer
randInt = ""
randInt = random.randint(1,2)
# Determines key use
keyUse = ""
# Sets the keys used ( Adds the determiner too "!" or "?")
if (randInt == 1):
encr_T = "!"
keyUse = keys1
elif(randInt == 2):
encr_T = "?"
keyUse = keys2
# Encrypt
char_T = input(f"Encrypt : ")
# Indexing
for letter in char_T:
index = chars.index(letter)
encr_T += keyUse[index]
print(f"Encrypted text : {encr_T}")
# Decrypt
encr_T = input("Decrypt : ")
char_T = ""
# Indexing
for letter in encr_T:
if (encr_T[0] == "!"):
index = keys1.index(letter)
elif (encr_T[0] == "?"):
index = keys2.index(letter)
char_T += chars[index]
print(f"Decrypted text : {char_T[1:]}")
Current progress remake
Having watched a couple of videos, read a couple of source codes, and learned a couple of other alternative ways to approach code, I applied
- avoiding nesting
- having actual functions to make code reusable
- keeping the long variable values in another file to keep the code clean
(I am aware of the random.shuffle()
function but I wanted to give it the ability to decrypt what I encrypted so I set an indicator of which set of keys are being used, which is why I have the 'encryption key' variable in key_checker()
and the decrypted text being printed from the index [:1]
.)
- use of dictionaries to avoid a longer code
- annotations to help me understand the parameters I am giving in
I also have main.py
that is running the code.
main.py
from work import user_input as up
if __name__ == '__main__':
while True:
up()
keys.py
import random
import string
key_choice = random.randint(1,3)
key1 = " " + string.punctuation + string.digits + string.ascii_letters
key_1 = list(key1)
key_2 = ['J', '0', '>', '(', '$', 'w', '/', '_', 'N', ',', 'c', ':', '+', '{', '2', '"', '|', '<', '5', 'v', '9', '\\', '&', ')', '~', 'Y', 'W', 'C', 'u', 'b', 'h', 'R', ';', 'o', 'U', '4', 'B', 'Z', '-', '6', 's', '}', 'E', 'y', '*', '@', 'V', '1', 't', 'n', 'm', '^', '[', '%', 'F', 'K', 'T', '?', ']', ' ', 'q', '`', 'X', '=', 'd', 'g', 'G', '7', '!', 'D', 'r', '3', 'x', 'A', 'l', 'H', '.', 'S', '8', 'j', 'L', 'M', 'O', 'a', 'Q', 'P', "'", 'z', 'i', 'e', 'k', '#', 'p', 'I', 'f']
key_3 = ['6', 'H', 'j', 'B', 'L', '~', 'F', '"', 'c', '>', '5', '3', '<', '9', 'w', '=', 'X', 'r', ')', '&', 'S', 'm', '/', 'x', 'u', 'W', 'n', '\\', 's', 'D', 'l', 'f', 'J', 'q', '0', 'z', 'I', 'M', 'A', '{', 'E', 'P', '|', 'Y', 'y', '^', '%', '#', 'k', '+', '?', 'Q', 'e', "'", ']', '.', 'G', '(', 'b', 't', '1', '!', 'R', 'V', '*', '4', 'O', ';', '-', '$', 'Z', 'i', 'a', '_', '[', ':', 'N', ',', '}', 'C', 'T', 'g', 'v', 'o', 'h', ' ', 'd', 'U', 'K', '8', '7', 'p', '@', '`', '2']
key_4 = ['B', 'o', 'C', '#', 'j', ':', 'S', '}', '<', 'W', 'l', '2', ')', 'f', '"', 'z', 'M', '`', '{', 'U', 'a', 'y', 't', 'p', 'm', '8', 'X', 'I', '6', 'w', 'Z', '.', 'G', '@', '3', 'N', '(', '!', ']', '_', '+', 'O', '^', ',', '0', 'u', '5', 'e', 'q', 'n', 'x', 'i', 'b', 'Y', '7', 'g', 'v', '[', ';', '>', '$', 'Q', '/', 'E', ' ', 'R', 'V', 'r', 'T', 'J', '=', '1', 'D', 's', '?', '~', 'h', '-', '&', '|', 'c', 'A', '*', 'k', '9', '\\', 'K', 'P', 'F', '4', 'H', '%', "'", 'd', 'L']
key_dict = {
'!': key_2,
'?': key_3,
'@': key_4,
}
work.py
from keys import key_1 as k1, key_2 as k2, key_3 as k3, key_4 as k4, key_choice as ks, key_dict as kd
def key_checker() -> tuple[str, list[str]]:
if ks == 1:
encryption_key = "!"
key_use = k2
elif ks == 2:
encryption_key = "?"
key_use = k3
else:
encryption_key = "@"
key_use = k4
return encryption_key, key_use
def user_input() -> str:
encryption_key, key_use = key_checker()
message_to_encrypt = input('\nEncrypt / Decrypt / Quit? : ').lower()
if message_to_encrypt == 'quit':
exit()
elif message_to_encrypt == 'encrypt':
encr_this = input('Encrypt this message : ')
start_encrypt(encr_this, encryption_key, key_use)
elif message_to_encrypt == 'decrypt':
decr_this = input('Decrypt this message : ')
start_decrypt(decr_this)
else:
print('Invalid Response!')
def start_encrypt(message_to_encrypt: str, encryption_key: str, key_use: list[str]) -> str:
for i in message_to_encrypt:
index = k1.index(i)
encryption_key += key_use[index]
print(f"\nEncrypted message : {encryption_key}")
def start_decrypt(message_to_decrypt: str) -> str:
clear_text = ""
key_use = kd.get(message_to_decrypt[0])
for i in message_to_decrypt:
index = key_use.index(i)
clear_text += k1[index]
print(f"Decrypted message : {clear_text[1:]}")
I have tried optimizing my code into something that runs better and more understandable for others, trying to improve in Python too.