Search code examples
pythonwords

I was trying to make it so something would equal something else Python 2.7.3


I decided to make a code with a friend and wanted to be able to crack (decode) it with this program. Basically what I want is one word to equal another for example the word "be" would show in the program as the word "ok".

So I have the raw_input to put the words to crack in. Say I put in the sentence "hi friend" how would I get it to come out as for example "dog crazy". Also if I put in "dog crazy" to come out as "hi friend".

Sorry if this is hard to understand but I'm not sure how to explain it to well.

I am using Python 2.7.3. Thanks.


Solution

  • Based on simonzack idea but with some improvements and fixing.

    mapping = {
        'hi':       'dog',
        'friend':   'crazy',
    }
    
    # Add to mapping dog->hi and friend->crazy automatically.
    mapping.update({v:k for k, v in mapping.items()})
    
    # Convert using mapping.
    print(' '.join([mapping.get(w, w) for w in raw_input().split()]))
    

    Verified:

    % python sol.py 
    hello crazy friend
    hello friend crazy
    
    % python sol.py
    crazy dog
    friend hi