So I’ve been working on a Caesar Cipher in python ever since @stetim94 mentioned it in my old challenges topic 22 days ago.
I gave up on it for a while so that I could just think about it rationally and figure it out, but my weekend has started, and I think that I’m ready to finish it. The only thing is that I have so much going on inside of it, that I need somebody who has a fresh mind on this to kind of help me out with what I’m having trouble with.
The program I want to build is one that takes user input and encrypts and decrypts based on what the user chooses. I haven’t even started the decryption yet, because I’m having trouble with the encryption, but I should be able to figure out the subtract fairly easily.
I want the user to enter a shift as well, and I want up to three numbers to be entered, so if the shift was ‘123’, and the text was ‘zzz’ it would return ‘abc’, basically I want to be able to have a key that shifts in a pattern.
This sounds easy enough, and I have been trying for a week or so, but I’m stuck right now. Not even regarding the logic of my program right now, I’m not even outputting the correct thing.
The other half of the issue is that I don’t know how to condense the pattern into a loop, and that’s probably the worst part of it all, because I’m drawing a major blank on where to start.
So here is my code. I know it’s not very good, so no laughing , just wondering if anybody has insight to share because this is kind of my weekend project.
choice = raw_input("Would you like to 'encrypt' or 'depcrypt'(Type e or d or QUIT to quit): ")
bad = " .,!?;"
while choice != "QUIT":
if choice == "e":
text = raw_input("Enter text: ")
shift = raw_input("Enter shift (numbers, up to a pattern of 3): ")
encodedStr = ""
if len(shift) == 1:
for letter in text.lower():
if letter not in bad:
if (ord(letter) + int(shift) < 122):
letter = chr(ord(letter) + int(shift))
else:
i = 0
num = int(shift)
while (ord(letter) + i <= 122):
num -= 1
i += 1
letter = chr(97 + num)
encodedStr += letter
print encodedStr
elif len(shift) in range(4):
shifts = []
for character in shift:
shifts.append(int(character))
for letter in text.lower():
for counter in range(1, len(text), 1):
for increase in range(1, len(shift), 1):
if letter not in bad:
if (ord(letter) + shifts[increase] < 122):
letter = chr(ord(letter) + shifts[increase])
else:
i = 0
num = shifts[increase]
while (ord(letter) + i <= 122):
num -= 1
i += 1
letter = chr(97 + num)
encodedStr += letter
print encodedStr
else:
print "Just up to three numbers, please."
elif choice == "d":
text = raw_input("Enter text: ")
shift = raw_input("Enter shift (numbers, up to a pattern of 3): ")
encodedStr = ""
if len(shift) == 1:
for letter in text.lower():
if letter not in bad:
if (ord(letter) - int(shift) >= 97):
letter = chr(ord(letter) - int(shift))
else:
i = 0
num = int(shift)
while (ord(letter) - i >= 97):
num -= 1
i += 1
letter = chr(122 - num)
encodedStr += letter
print encodedStr
else:
print "That's not a valid choice!"
choice = raw_input("Would you like to 'encrypt' or 'depcrypt'(Type e or d or QUIT to quit): ")
print "Cryptation complete!"
EDIT: Updated my code, added some stuff, (decryption of a static shift tried to figure out the loop), but I’m still having a heck of a time so any advice on the loop would be spectacular.