Hi guys, I'm learning python right now, and I'm having a little trouble with one of my assignments. I have to write a program that will take a word and "encrypt" it. an example, if the letter is 'a' then it should get converted to 'b'. I have the code done for the first part (first assignment was just to do one character) right here

def translateChar(char,key):
    abc=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
         'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
    if char in abc:
        str=ord(char)
        if str > 64 and str < 91: 
            if key == 'a':
                str+=0
                char=chr(str)
                return char
            elif key == 'b':
                str+=1
                if str > 90:
                    temp=str-90
                    str=64+temp
                char=chr(str)
                return char
            elif key == 'c':
                str+=2
                if str > 90:
                    temp=str-90
                    str=64+temp
                char=chr(str)
                return char
            else:
                return 'Key out of Range'
        elif str > 96 and str < 123:
            if key == 'a':
                str+=0
                char=chr(str)
                return char
            elif key == 'b':
                str+=1
                if str > 122:
                    temp=str-122
                    str=96+temp
                char=chr(str)
                return char
            elif key == 'c':
                str+=2
                if str > 122:
                    temp=str-122
                    str=96+temp
                char=chr(str)
                return char
            else:
                return 'Key out of Range'
    else:
        return 'Character must be in the alphabet'

so for the second one I have to let take a whole word (or sentence) and it should still convert them all the same way. I tried using the above code with a for loop, heres the new code

def translatePhrase(phrase,key):
    abc=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
         'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
    for char in phrase:
        print char
        if char in abc:
            str=ord(char)
            if str > 64 and str < 91: 
                if key == 'a':
                    str+=0
                    char=chr(str)
                elif key == 'b':
                    str+=1
                    if str > 90:
                        temp=str-90
                        str=64+temp
                    char=chr(str)
                elif key == 'c':
                    str+=2
                    if str > 90:
                        temp=str-90
                        str=64+temp
                    char=chr(str)
                else:
                    return 'Key out of Range'
            elif str > 96 and str < 123:
                if key == 'a':
                    str+=0
                    char=chr(str)
                elif key == 'b':
                    str+=1
                    if str > 122:
                        temp=str-122
                        str=96+temp
                    char=chr(str)
                    print char
                
                elif key == 'c':
                    str+=2
                    if str > 122:
                        temp=str-122
                        str=96+temp
                    char=chr(str)
                else:
                    return 'Key out of Range'
        else:
            return 'Character must be in the alphabet'
    print phrase

As its running through I can see each character gets changed (print statements in the code for debugging) but when it gets to the end it doesn't actually save the changes it made. I'm trying to get help on my school website, but answers are really slow on there. Any help would be greatly appreciated

Somewhere in your loop you have to build up your encrypted phrase. Also, a phrase will contain spaces, so you need to take care of that.

how would I go about doing that? I think I can get away with just one word phrases, at least for now, I'll ask if I need to worry about spaces or not.

This is how you would build up your encrypted phrase (simplified example):

phrase = 'hello'

shiftby = 1
new_phrase = ""
for c in phrase:
    # create shifted char
    new_c = chr(ord(c) + shiftby)
    # build up new phrase
    new_phrase += new_c

print phrase       # --> hello
print new_phrase   # --> ifmmp

Also abc does not have to be a list it can simply be a string like this:

import string

#help(string)
abc = string.ascii_letters
print abc  # abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

okay cool, I think I can get that implemented pretty easily, thanks!

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.