I'm attempting to create a one line Caesar cipher decryption program in python and i'm afraid i've come to the end of my knowledge and well past it.

this is the closest working code i've got:

def test3(): 
    print("The decrypted text is: "+"".join(map(lambda x: chr(ord(x)+y),x)))

as you can see it requires me to define x and y (the input text and the shift amount respectively) before hand. this wont do, next i tried this (and several variations of)

def test4(): 
    print("The decrypted text is: "+"".join(map(lambda x=input("text: "),y=eval(input("shift: ")): chr(ord(x)+y),x)))

but x is undefined globally

the function should run as follows:

>>>text: |nqq
>>>shift: -5
The decrypted text is: will

any help would be greatly appreciated
ps. using python 3.1.3

Why do not use the function you defined by having parameters and return value instead of print? int is better (safer) than eval on user input.

Edited 5 Years Ago by pyTony: n/a

i've updated the code to:

def test8(): #input works
    z=lambda x=input("Enter the text to be decrypted: "),y=int(input("Enter the shift amount: ")): print("The decrypted text is: "+"".join(list(map(lambda x: chr(ord(x)+y),x))))
    return z()

is this what you meant?
also is there anyway i can do this without the return z() line?
i tried just replacing z=lambda... with return lambda... but i just get something along these lines:

<function <lambda> at 0x0000000003A698C8>

This is not Lisp however much it is my first love in programming languages, study about defining functions (or procedures, they are same in Python), also study about generators and use join as you have already done to get result.

Input part cannot be counted as part of one liner function, but is part of calling the function like:

encrypted = caesar(original=input("Enter the text to be decrypted: "), shift=int(input("Enter the shift amount: ")))

Notice the descriptive parameter names.

Hi,
You can use () to make it work (if you really want a one liner)...

def l():
    return (lambda ...)()
def l():
    return (lambda x=input("Enter the text to be decrypted: "),y=int(input("Enter the shift amount: ")): print("The decrypted text is: "+"".join(list(map(lambda x: chr(ord(x)+y),x)))))()

thank you both for the help, jice, it worked perfectly thanks!
and tonyjv i decided take your advice and prettied up my variable names a bit

final code:

def decoder(): #SUCCESS
    return (lambda encryptedText=input("Enter the text to be decrypted: "),shift=int(input("Enter the shift amount: ")): print("The decrypted text is: "+"".join(list(map(lambda encryptedText: chr(ord(encryptedText)+shift),encryptedText)))))()
This question has already been answered. Start a new discussion instead.