-2

I have to create a function that takes a message the user wants to encrypt and returns the encrypted string. The encryption follows a set of rules:

  • Character: In the alphabet and uppercase [A-Z], Replace with: Lowercase character and add a ^ character afterwards
  • Character: In the alphabet and lowercase [a-z], Replace with: Do not change
  • Character: 1, Replace with: @
  • Character: 2, Replace with: #
  • Character: 3, Replace with: $
  • Character: Any other character, Replace with: *

This is my code so far. It'll only return 'p^', so I figured something is wrong with my first if statement and the return value. If someone could steer me in the right direction I'd appreciate it!

def encryptMessage(secretMsg):

secretMsg = str(secretMsg)
u = secretMsg.upper()
symbol = "^"
for x in secretMsg:
    if u in secretMsg:
        return u.lower() + symbol
        secretMsg += x
    return secretMsg
    if u.lower():
        return secretMsg
    elif 1 in secretMsg:
        return secretMsg.replace(1, "@")
    elif 2 in secretMsg:
        return secretMsg.replace(2, "#")
    elif 3 in secretMsg:
        return secretMsg.replace(3, "$")
    num = u > 3
    if num in secretMsg:
        return secretMsg.replace(num, "*")
return secretMsg

Examples:

Note: These are examples of what the program should return, not actual instances of the program running.

>>> encryptMessage("my123password")
'my@#$password'
>>> encryptMessage("Dan()123Barrun")
'd^an**@#$b^arrun'
>>> encryptMessage("PASS99cats")
'p^a^s^s^**cats'
Zenohm
  • 548
  • 6
  • 17
swimdawg12
  • 83
  • 1
  • 1
  • 6
  • That's quite an impressive "encryption" algorithm... real James Bond security. – Nathan Tuggy Sep 07 '15 at 02:11
  • More seriously, please [edit] to add a specific problem statement — "it doesn't work" can be assumed, but *how* does it not work? What error message or incorrect behavior is characteristic? – Nathan Tuggy Sep 07 '15 at 02:11
  • @NathanTuggy It's not meant to be impressive, just something for class. – swimdawg12 Sep 07 '15 at 02:21

1 Answers1

1

Let's start from the top and work our way down.

First off, this is not encryption.

This is just adding random characters to text, not converting the data into cipher-text which can then be decrypted via a key. If anything I'd call it obfuscation.


Now that we have that out of the way, let's tell you why your code is not working.

def encryptMessage(secretMsg):
    secretMsg = str(secretMsg)
    u = secretMsg.upper()
    symbol = "^"
    for x in secretMsg:
        if u in secretMsg:
            return u.lower() + symbol # Your function stops here b/c return
            secretMsg += x
        return secretMsg # Use print() to display, return will stop your function.
        # Nothing below here is executed because of the return above.
        if u.lower(): # This is always True unless u is empty.
            return secretMsg # Function will then stop here.
        elif 1 in secretMsg: # This will raise an error, make that 1 a string
            return secretMsg.replace(1, "@") # Same here
        elif 2 in secretMsg: # here
            return secretMsg.replace(2, "#") # here
        elif 3 in secretMsg: # here
            return secretMsg.replace(3, "$") # and here
        num = u > 3 # This will return True if u > 3 or False if u <= 3, not any number greater than u.
        if num in secretMsg:
            return secretMsg.replace(num, "*")
    return secretMsg

Please research the return statement through this stackoverflow post or through this video.


Since you were appreciative of feedback, I have decided to go one step further in helping and am going to give you a better way to do the program so you can learn from it.

import string # This module holds lists of characters that you would find useful.

def encryptMessage(original_message):
    original_message = str(original_message)
    secret_message = []
    conversions = {"1": "@", "2": "#", "3": "$"}
    for letter in original_message:
        if letter in string.ascii_uppercase:
            secret_message.append(letter.lower() + "^")
        elif letter in '123':
            secret_message.append(conversions[letter])
        elif letter in string.ascii_lowercase:
            secret_message.append(letter)
        else:
            secret_message.append("*")
    return ''.join(secret_message)

If you want to know what the variable conversions is, it is known as a dictionary.

If you want to know what the [] brackets denote, they denote a list. Note that .append() is a function that adds a new element on to the end of a list or compatible data structure, and that ''.join() is a function which joins a list together into a string.

If you want to know what I'm doing with string.ascii_uppercase, I'm calling a variable defined in the string module. For more information on modules, please look at the documentation.

For more information on how to work with Python's control flow statements, please, please, please read up on them here.

Zenohm
  • 548
  • 6
  • 17
  • Thanks for the feedback, and I'd just like to tell you that I didn't create this assignment. I understand that it's nothing like encryption so please tell that to the dude who made it. I found the function quite stupid to be honest but hey, I have to do it. Thanks again. – swimdawg12 Sep 07 '15 at 02:59
  • @swimdawg12 Since you were quite a ways off with your first attempt, I've gone ahead and written a working program. However, remember that you must study the code on your own and learn how everything works otherwise none of this will do you any good. Just use the references that I've given you and read through each line of the code, character by character, until you know exactly what the program does and how it does it. – Zenohm Sep 07 '15 at 03:38
  • I read through it and understand what's happening. I really do appreciate all your help as it was an important learning experience as well. I haven't learned many of the functions you've used yet but I can see how helpful they are for this function. Thanks again.:) – swimdawg12 Sep 07 '15 at 04:13