This might be easier to handle with a list
:
encoded = list(Encoded_Team)
plaintext = list(Team)
changes = []
while encoded != plaintext:
print("\nThe encoded team is {0}\n".format("".join(encoded)))
old = input("Which letter would you like to replace? ")
indices = [i for i, c in enumerate(encoded) if c == old]
new = input("What letter would you like to replace it with? ")
for i in indices:
encoded[i] = new
changes.append((old, new, indices))
Note the "list comprehension", which is a short-hand version of the following:
indices = []
for i, c in enumerate(encoded):
if c == old:
indices.append(i)
Now you can easily reverse the operations, even if choose
was already in encoded
:
for old, new, indices in changes:
print("Replaced '{0}' with '{1}'".format(old, new))
undo = "Would you like to undo that change (y/n)? ".lower()
if undo == "y":
for i in indices:
encoded[i] = old