I am struggling with commenting and variable naming. My teacher says my comments need to be more explanatory and explicit. He says my variable names are also sometimes confusing. I was just wondering whether you could go through my code, see whether you are able to understand the comments and whether the variables names are appropriate. And of course, suggest improvements where you feel they are needed.
#Caesar Cipher
#Function that reads and writes data to the file, and calls the encryption and decryption methods.
#It isolates the key and choice of the user, and then calls the appropriate function.
def isolate():
#Open input file in read mode
fin = open('Input3.txt', 'r')
#Open output file in write mode
fout = open('Out1.txt', 'w')
#Create a list that holds each line of the file as an element of the list
container = fin.readlines()
#For loop that goes through each line of the file (contianed in list) and isolates the choice and key of the user
#Then it removes the key and choice from the split input list and calls the appropriate method.
for i in container:
#Each element of the list becomes a list (words) with all spaces and '\n' removed
words = i.split()
#The key of the user is always the last element of the split() list.
key = int(words[len(words) - 1])
#The choice of the user is always the second last element of the split() list.
choice = words[len(words) - 2]
#Remove the key and choice from the message
words.remove(str(key))
#Remove the choice of the user from the message for encryption/decryption.
words.remove(choice)
#Join together all the elements of the message in a string
message = ' '.join(words)
message = message.upper()
#If the user wishes to encrypt, call the encrypt method. This method will return the encrypted message as a sting, which can then be written to the output file.
if choice == 'e':
#Write the newly encrypted message to the output file.
fout.write(encrypt(message, key) + '\n')
#If the user wishes to decrypt, call the decrypt method. This method returns the decrypted message as a string, which can then be written to the output file.
else:
fout.write(decrypt(message, key) + '\n')
#Close the file to make sure data is written properly to the file.
fout.close()
#Encryption method, which takes two parameters, the users message and key, and returns the newly encrypted message.
def encrypt(message, key):
#Empty string that will contain the encrypted message.
encrypted_message = ""
#Alphabet string, which contains all the letters of the alphabet at their index locations.
alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
#For loop that goes through each character of the users message and, if a space, adds it straight to the encrypted message or encrypts it through modular division.
for i in message:
#If the character is a space, add it to the final message.
if i == ' ':
#Concatenate the space to the final message
encrypted_message += ' '
#If the character is not a space, determine the final locatin out of index range, % by 26 and re-add the character at this index to the encrypted_message.
else:
#Determine the final location of the character (out of index range)
location = key + alpha.index(i)
#% this location by 26 to determine the location within the proper index.
location %= 26
#Finally, concatenate the letter at this location to the final message.
encrypted_message += alpha[location]
#When the function is called, have it return the final encrypted message.
return encrypted_message
#Decryption method that takes two parameters, the users message and key, and returns the newly decrypted message.
def decrypt(message, key):
#Create a string that reverses the alphabet, since we are moving backwards when we decrypt.
reverse_alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[::-1]
#Empty string that will hold the decrypted message.
decrypted_message = ""
#For loop that runs through each element of the users message and, if space, re adds it to the message, or encrypts using % division and concatenates to final message.
for i in message:
#If the element is a space, do not encrypt; concatenate it to the final message.
if i == ' ':
#Concatenate the space to the final message.
decrypted_message += ' '
#If the element is not a space, encrypt the letter.
else:
#Determine the final location out of index range of the encrypted character, and store it in the variable 'location'
location = key + reverse_alpha.index(i)
#% this position by 26 to determine the index location in range of the letter.
location %= 26
#Finally, add the letter at this location to the decrypted message.
decrypted_message += reverse_alpha[location]
#Converts the decrypted message into lowercase form.
decrypted_message = decrypted_message.lower()
#When the function is called, have it 'give back' the decrypted message so that it can be written to the output file.
return decrypted_message
#Call the function isolate5(), which initiates the program.
isolate()
#Open input file in read mode
the function open explains itself. Maybe change thei
infor i in container:
toword
or something. Personaly I think there's too many comments \$\endgroup\$return
statement. ` #When the function is called, have it 'give back'...` \$\endgroup\$