Hello everyone.
Today I decided to write a little Caesar cipher script in Python. After tweaking the code and testing it, I shot up my browser and had a look at other snippets on the web. I was shocked at what I saw.
All of the Python solutions I went over were implemented in ~30 LoC. My implementation is ~160 LoC. I was aware of my "descriptive" coding style i.e. I'm not really good at making my code shorter and more concise, but this was too much. A 130 LoC difference? Goddammit.
I'll have to admit it though: my script can encipher/decipher both words and sentences that can contain a mix of upper and lower-case letters. I'm not sure whether or not the scripts I saw did this as well, but I'm certain that this can be added in less than ~30 LoC.
Well then, here it is. Scary, is it not?
# Sorry for the lack of comments.
from sys import exit
lowercase = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
uppercase = list(''.join(lowercase).upper())
def encrypt_caesar_cipher():
sentence = raw_input("\nEnter a sentence to encrypt: ")
shift = input("Enter a key (this is used to decrypt the message): ")
if ' ' in sentence:
words = sentence.split()
encrypted_words = []
else:
letters = str(sentence)
encrypted_letters = []
for letter in letters:
if letter in lowercase:
index = lowercase.index(letter)
if index + shift < 26:
index += shift
encrypted_letters.append(lowercase[index])
elif index + shift >= 26:
index += shift
while index > 25:
index -= 26
encrypted_letters.append(lowercase[index])
else:
pass
elif letter in uppercase:
index = uppercase.index(letter)
if index + shift < 26:
index += shift
encrypted_letters.append(uppercase[index])
elif index + shift >= 26:
index += shift
while index > 25:
index -= 26
encrypted_letters.append(uppercase[index])
else:
pass
else:
print "\nPlease use upper or lower case letters ONLY next time."
exit(0)
print '\n' + ''.join(encrypted_letters)
exit(0)
for word in words:
letters = str(word)
encrypted_letters = []
for letter in letters:
if letter in lowercase:
index = lowercase.index(letter)
if index + shift < 26:
index += shift
encrypted_letters.append(lowercase[index])
elif index + shift >= 26:
index += shift
while index > 25:
index -= 26
encrypted_letters.append(lowercase[index])
else:
pass
elif letter in uppercase:
index = uppercase.index(letter)
if index + shift < 26:
index += shift
encrypted_letters.append(uppercase[index])
elif index + shift >= 26:
index += shift
while index > 25:
index -= 26
encrypted_letters.append(uppercase[index])
else:
pass
else:
print "\nPlease use upper or lower case letters ONLY next time."
exit(0)
result = ''.join(encrypted_letters)
encrypted_words.append(result)
encrypted_sentence = ' '.join(encrypted_words)
print "\n" + encrypted_sentence
def decrypt_caesar_cipher():
encrypted = raw_input("\nEnter a sentence to decrypt: ")
shift = input("Enter the key: ")
if ' ' in encrypted:
words = encrypted.split()
decrypted_words = []
else:
letters = str(encrypted)
decrypted_letters = []
for letter in letters:
if letter in lowercase:
index = lowercase.index(letter)
if index - shift >= 0:
index -= shift
decrypted_letters.append(lowercase[index])
elif index - shift < 0:
index -= shift
while index <= 0:
index += 26
decrypted_letters.append(lowercase[index])
else:
pass
elif letter in uppercase:
index = uppercase.index(letter)
if index - shift >= 0:
index -= shift
decrypted_letters.append(uppercase[index])
elif index - shift < 0:
index -= shift
while index <= 0:
index += 26
decrypted_letters.append(uppercase[index])
else:
pass
print '\n' + ''.join(decrypted_letters)
exit(0)
for word in words:
letters = str(word)
decrypted_letters = []
for letter in letters:
if letter in lowercase:
index = lowercase.index(letter)
if index - shift >= 0:
index -= shift
decrypted_letters.append(lowercase[index])
elif index - shift < 0:
index -= shift
while index <= 0:
index += 26
decrypted_letters.append(lowercase[index])
else:
pass
elif letter in uppercase:
index = uppercase.index(letter)
if index - shift >= 0:
index -= shift
decrypted_letters.append(uppercase[index])
elif index - shift < 0:
index -= shift
while index <= 0:
index += 26
decrypted_letters.append(uppercase[index])
else:
pass
result = ''.join(decrypted_letters)
decrypted_words.append(result)
new_sentence = ' '.join(decrypted_words)
print "\n" + new_sentence
ans = raw_input("\nDecrypt or encrypt?: ")
if ans == 'encrypt':
encrypt_caesar_cipher()
elif ans == 'decrypt':
decrypt_caesar_cipher()
else:
print "\nWhat?"
Does this unusual habit of mine make my coding inefficient? Should I change my ways, so I don't become cursed with bad coding habits for the rest of my life? If so, are there any books on coding practices you would recommend?
Please. I'm really worried about where this is might be headed. Any advice or help would be appreciated.
Thanks.