EvilZone

Programming and Scripting => Scripting Languages => : vezzy December 31, 2012, 11:17:46 PM

: [Python] Caesar Cipher + Assistance Required
: vezzy December 31, 2012, 11:17:46 PM
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?
: Re: [Python] Caesar Cipher + Assistance Required
: Deque January 01, 2013, 12:15:04 PM
Use upper()

:
'foo'.upper()
'FOO'
: Re: [Python] Caesar Cipher + Assistance Required
: parad0x January 01, 2013, 12:35:24 PM
Use upper()

:
'foo'.upper()
'FOO'
But you said that you don't do python. >:( >:(
: Re: [Python] Caesar Cipher + Assistance Required
: vezzy January 01, 2013, 06:58:07 PM
Use upper()

:
'foo'.upper()
'FOO'

That's another way to do it. Thank you. I took a look at all available string methods now, so I'll keep them in mind.
: Re: [Python] Caesar Cipher + Assistance Required
: Deque January 01, 2013, 09:20:27 PM
But you said that you don't do python. >:( >:(

I don't, but I am able to read the API.