0

I found this code while browsing in the internet. It's just a simple Caesar Encoding and Decoding thing. But I want it to reverse the word. But It doesn't work. I wonder why...

    def machine():

    keys = 'abcdefghijklmnopqrstuvwxyz'

    values = keys[-2] + keys[0:-2]
    reversedx = values.reverse()

    encrytDict = dict(zip(keys, values, reversedx))
    decryptDict = dict(zip(reversedx, values, keys))

    message = input("Enter your message ")
    mode = input("Encode(E) Decode(D): ")

    if mode.upper() == 'E':
        newMessage = ''.join([encrytDict[letter]
                              for letter in message.lower()])
    elif mode.upper() == 'D':
        newMessage = ''.join([decryptDict[letter]
                              for letter in message.lower()])                         
    else:
        print("Wrong!")

    return newMessage.capitalize()


    print(machine())
  • please fix the indentation of the code posted. – Kaushal Sharma Oct 22 '21 at 15:42
  • 1
    What do you mean exactly by saying 'it doesn't work?' – Chris Oct 22 '21 at 15:42
  • 1
    `reverse` doesn't return the reversed list; it reverses in-place and returns `None`. – chepner Oct 22 '21 at 15:42
  • This should not be closed @chepner under reversing a list, `values` is a string, not a list. You need to use `values[::-1]` to reverse the value. – PacketLoss Oct 22 '21 at 15:44
  • `values` is a string, not a list. You can reverse it with `values[::-1]`. – bwdm Oct 22 '21 at 15:46
  • I missed that `values` is a `str`, not a `list`, but `str.reverse` doesn't even exist, and `reverse(values)` works just as well on a `str` as a `list`. Given the lack of details about what *else* is wrong with the code, I don't see any reason to keep it open just because the duplicate only applies to the first and most obvious problem. – chepner Oct 22 '21 at 15:48

0 Answers0