-1

I am making my own substitution cipher with python, and am trying to use the replace function. It seems as if it does not work though...

print("Scylla\n\n")
w = input("Encode (E) or Decode (D)?\n\n")
x = str(input("What is the message?\n\n"))
y = str(x.upper())
if w == "E":
    for char in y:
        y.replace("A", "N")
        y.replace("B", "O")
        y.replace("C", "P")
        y.replace("D", "Q")
        y.replace("E", "R")
        y.replace("F", "S")
        y.replace("G", "T")
        y.replace("H", "U")
        y.replace("I", "V")
        y.replace("J", "W")
        y.replace("K", "X")
        y.replace("L", "Y")
        y.replace("M", "Z")
        y.replace("N", "A")
        y.replace("O", "B")
        y.replace("P", "C")
        y.replace("Q", "D")
        y.replace("R", "E")
        y.replace("S", "F")
        y.replace("T", "G")
        y.replace("U", "H")
        y.replace("V", "I")
        y.replace("W", "J")
        y.replace("X", "K")
        y.replace("Y", "L")
        y.replace("Z", "M")
    print(y)

But all it does is print the phrase input in caps. What am I doing wrong?

1 Answers1

0

The replace function works fine. You're just using it wrong. It returns a new string with the replacement made, rather than updating the old one in place, so you need to save its return value. (And in general, your first reaction should always be "I'm using this standard, common thing wrong" rather than "this standard, common thing is broken".)