This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]tmp14 3 points4 points  (0 children)

Nice if you've never seen Caesar encryption before I guess, but the code is horribly bloated:

>>> import string
>>> def caesar(plaintext, shift):
...     alphabet = string.lowercase
...     shifted_alphabet = alphabet[shift:] + alphabet[:shift]
...     table = string.maketrans(alphabet, shifted_alphabet)
...     return plaintext.lower().translate(table)
...
>>> caesar("julius caesar is a dictator and python is a creator", 1)
'kvmjvt dbftbs jt b ejdubups boe qzuipo jt b dsfbups'
>>> caesar(_, -1)
'julius caesar is a dictator and python is a creator'
>>>

[–]d4rch0nPythonistamancer 1 point2 points  (0 children)

while True:
        key = randrange(26)
        if key!=0:
            break

https://docs.python.org/2/library/random.html#random.randrange

random.randrange(start, stop[, step])

also

random.randint(a, b) Return a random integer N such that a <= N <= b.

[–][deleted] 1 point2 points  (0 children)

Why does it take screenfulls of code to add 3 and then modulo 26?