Ive created a dictionary with all the letters of the alphabet, assigning each one to another random letter.

encode={'b':'a','B':'A','c':'e','C':'E','d':'i','D':'I','f':'o','F':'O','g':'
u','G':'U','h':'b','H':'B','j':'c','J':'C','k':'d','K':'D','l':'f','L':'F','m
':'g','M':'G','n':'h','N':'H','p':'j','P':'J','q':'k','Q':'K','r':'l','R':'L'
,'s':'m','S':'M','t':'n','T':'N','v':'p','V':'P','w':'q','W':'Q','x':'r','X':
'R','y':'s','Y':'S','Z':'t','z':'T','a':'v','A':'V','e':'w','E':'W','i':'x','
I':'X','o':'y','O':'Y','u':'Z','U':'z',' ':' ','!':'!'}

Now what Im trying to do is to create a single function that takes the input, does the calculation, and produces the output, using the letter from the input to be the key of the value.

What I have in mind is to convert the dictionary into a list, then use the list to look up the user input and encode the message. This idea doesnt seem to be very efficient.

Any other suggestions?

Just use your dictionary, but the last key ':','!', which is tuple when all other keys are string, looks incorrect.

Well Im having some issues with this. We finished doing dictionaries in class and Im trying to get a better understanding of dictionaries.

The issue Im having is that I have a function:

def user_input():        
    message = raw_input ("Please enter a phrase: ")
    message = list(message)
    print message

This converts the user input into a list and Im trying to use that list of input to return the value from the dictionary.

For example, if the user inputs 'computer' I want the function to return 'eygjznwl'.

I apologize for my noobyness >.>

It is difficult to go forward without giving ready answer, but now I can give you one geeky generator expression shrinking your dictionary by checking for upper and lower version of letter and changing the case according to test. If you can catch some idea from here and put it in terms of your current knowledge (probably basic for loops and function instead of unwieldy conditional expression) so go ahead.

# make Python2 work Python3 style
from __future__ import print_function

try:
    input, range = raw_input, xrange
except:
    pass

encode = dict((key, value) for key, value in
              zip('abcdefghijklmnopqrstuvwxyz', 'vaeiwoubxcdfghyjklmnzpqrst'))

# sanity check assert
assert len(encode) == len(set(encode.values()))

print(''.join((encode[c] if c in encode else
                  (encode[c.lower()].upper() if c.lower() in encode else
                   c)
               )
              for c in input ("Please enter a phrase: ")))

This maps however u to lower case z not Z like your dictionary.

It works!!

Haha... Getting excited over code makes me feel geeky... lol

Simply awesome.

Anyway, there are a few things Im not understanding though, if maybe you could elaborate on it a bit.

from __future__ import print_function
try:
     input, range = raw_input, xrange
except:
     pass

what exactly is this doing? I have the basic idea thanks to the text book, but Ill def have to do some more reading up on this.

If you want simple codes to run both for Python 2 and Python 3 users, you can import new function style print from __future__ and define the input to be not evaluating raw_input (hiding the normal unsafe input in Python 2) and using range for xrange, which does not produce whole list but generates values one by one as needed. I did quite a few edits so check if you looked my final version with dict produced with zipped strings and assertion to check for mistakes in coding.

If you want simple codes to run both for Python 2 and Python 3 users, you can import new function style print from __future__ and define the input to be not evaluating raw_input (hiding the normal unsafe input in Python 2) and using range for xrange, which does not produce whole list but generates values one by one as needed. I did quite a few edits so check if you looked my final version with dict produced with zipped strings and assertion to check for mistakes in coding.

Hey, I just wanted to say thanks, you were a life saver.

Stayed up all night finishing that code and a pascal triangle code... I nearly went crazy.

Anyway thanks again for all the help, very much appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.