So I was feeling bored and decided to search for ROT-13 implementations in Python. I stumbled upon this:
import string
letters = string.ascii_lowercase
key = 13
trans_table = letters[key:] + letters[0:key]
trans = string.maketrans(letters, trans_table)
text = "i am crazy"
print text.translate(trans)
So I decided to modify it into a general Caesar cipher input/output script. End result I made (complete with arbitrary exception handling):
import string
print "--[Basic Caesar Cipher Input/Output]--\n"
try:
value = int(raw_input("Type in shift value (1-26): "))
except ValueError:
print "Type in a number."
exit(0)
letters = string.letters
convert_table = letters[value:0] + letters[0:value]
convert = string.maketrans(letters, convert_table)
print "\nEnter a string to encode/decode:"
while True: #infinite loop so user will keep receiving prompts
try:
text = str(raw_input("> "))
except KeyboardInterrupt:
print "\nSee ya."
exit(0)
print "Result:", text.translate(convert).title()
So it works, except for the fact that the output is in mixed case letters. I attempted several ways to fix it, but to no real avail. Finally, I settled on using the built-in title() function, which obviously capitalizes every first letter. I find that to be a kludge and rather unsatisfactory. Anyone got any ideas? Still a beginner here, but I think the slicing operator might be an issue?