patternpythonMajor
Reading and writing data for Caesar cipher
Viewed 0 times
readingcaesarwritingforanddatacipher
Problem
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 ==
```
#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 ==
Solution
Commenting every single line is excessive by most programmers' norms. There is a coding style called literate programming that heavily emphasizes comments, but even in literate programming, commenting every line would be frowned upon.
The most important kind of comment in Python is the docstring, and you didn't write any.
Many of your comments just repeat self-explanatory code. For example, this comment does nothing but double the amount of text a programmer has to read:
If you really had to guide a novice, you could make the comment less obtrusive by relocating it to the end of the line:
It's usually easy to see what each line of code does individually. However, comments can be very useful in providing the big picture. You would also be able to do a better job of describing intentions by explaining several lines of code at once.
Improving the naming and comments without changing the code at all:
The most important kind of comment in Python is the docstring, and you didn't write any.
Many of your comments just repeat self-explanatory code. For example, this comment does nothing but double the amount of text a programmer has to read:
#Open input file in read mode
fin = open('Input3.txt', 'r')If you really had to guide a novice, you could make the comment less obtrusive by relocating it to the end of the line:
fin = open('Input3.txt', 'r') # 'r' = read-onlyIt's usually easy to see what each line of code does individually. However, comments can be very useful in providing the big picture. You would also be able to do a better job of describing intentions by explaining several lines of code at once.
Improving the naming and comments without changing the code at all:
def run_caesar_cipher_batch():
"""Processes Input3.txt to generate Out1.txt.
Input3.txt contains input lines of the form
The quick brown dog e 3
VJG SWKEM DTQYP FQI d 3
The last two words of each line are:
'e' or 'd', indicating the choice to encrypt/decrypt, and
an integer, which acts as the Caesar cipher key.
Within each line, all whitespace, even multiple consecutive
whitespace characters, is treated as a single space.
"""
fin = open('Input3.txt', 'r')
fout = open('Out1.txt', 'w')
input_lines = fin.readlines()
for line in input_lines:
# List of words, with whitespace and trailing '\n' removed
words = line.split()
# Interpret and remove the last two words
key = int(words[len(words) - 1])
operation = words[len(words) - 2]
words.remove(str(key))
words.remove(operation)
# Re-join the words of the message. The message may differ from
# the original if there is consecutive whitespace.
message = ' '.join(words)
message = message.upper()
# Encrypt or decrypt to fout
if operation == 'e':
fout.write(encrypt(message, key) + '\n')
else:
fout.write(decrypt(message, key) + '\n')
fout.close()Code Snippets
#Open input file in read mode
fin = open('Input3.txt', 'r')fin = open('Input3.txt', 'r') # 'r' = read-onlydef run_caesar_cipher_batch():
"""Processes Input3.txt to generate Out1.txt.
Input3.txt contains input lines of the form
The quick brown dog e 3
VJG SWKEM DTQYP FQI d 3
The last two words of each line are:
'e' or 'd', indicating the choice to encrypt/decrypt, and
an integer, which acts as the Caesar cipher key.
Within each line, all whitespace, even multiple consecutive
whitespace characters, is treated as a single space.
"""
fin = open('Input3.txt', 'r')
fout = open('Out1.txt', 'w')
input_lines = fin.readlines()
for line in input_lines:
# List of words, with whitespace and trailing '\n' removed
words = line.split()
# Interpret and remove the last two words
key = int(words[len(words) - 1])
operation = words[len(words) - 2]
words.remove(str(key))
words.remove(operation)
# Re-join the words of the message. The message may differ from
# the original if there is consecutive whitespace.
message = ' '.join(words)
message = message.upper()
# Encrypt or decrypt to fout
if operation == 'e':
fout.write(encrypt(message, key) + '\n')
else:
fout.write(decrypt(message, key) + '\n')
fout.close()Context
StackExchange Code Review Q#36078, answer score: 29
Revisions (0)
No revisions yet.