I am trying to create a program that replaces word from a string.
ColorPairs = {'red':'blue','blue':'red'}
def ColorSwap(text):
for key in ColorPairs:
text = text.replace(key, ColorPairs[key])
print text
ColorSwap('The red and blue ball')
# 'The blue and blue ball'
instead of
# 'The blue and red ball'
This program replaces 'red' to 'blue', but not 'blue' to 'red'.I am stuck trying to figure out a way to make it so that the program doesn't override the first replaced key.