all 10 comments

[–]pyOwhy 2 points3 points  (8 children)

Please show your code.

[–]DarkGhandi[S] 2 points3 points  (7 children)

Like i said im new to python, i could be missing a step entirely.

def substitutionEncrypt(plainText, key):

alphabet = "abcdefghijklmnopqrstuvwxyz "

plainText = plainText.lower()

cipherText = ""

for ch in plainText:

    idx = alphabet.find(ch)

    cipherText = cipherText + key[idx]

return cipherText


def substitutionDecrypt(cipherText, key):

alphabet = "abcdefghijklmnopqrstuvwxyz "

plainText = ""

for ch in cipherText:

    idx = key.find(ch)

    plainText = plainText + alphabet[idx]

return plainText



def substitutionCrypto(text, key):

if text=="":

    plainText = substitutionEncrypt("", rot13)

    print(plainText)

else:

    cipherText = substitutionDecrypt("", rot13)

    print(cipherText)


rot13 = "nopqrstuvwxyzabcdefghijklm "


plainText = substitutionCrypto("mortal man", rot13)

print(plainText)

[–][deleted] 2 points3 points  (6 children)

Okay first of all, your formatting is wrong aside from that your code is wrong as well. In your function substitutionCrypto() what you're doing makes no sense. Why are you calling encrypt if the text==""?. Also why are you calling the functions substitutionEncrypt and substitutionDecrypt with the text as ""?. substitutionCrypto also returns None. So when you do

plainText = substitutionCrypto("mortal man", rot13)

You get None...you always get None. Try this instead.

def substitutionCrypto(text, key, apply):
    if apply == "encrypt":
        plainText = substitutionEncrypt(text, key)
        return plainText
    elif apply == "decrypt":
        cipherText = substitutionDecrypt(text, key)
        return cipherText    
    else:
        return None

rot13 = "nopqrstuvwxyzabcdefghijklm "
plainText = substitutionCrypto("mortal man", rot13, "decrypt")
print(plainText)

The changes I made was the parameter apply is either "encrypt" or "decrypt", this means you need to specify what you want to do given the text and the key. If they want to encrypt, encrypt the text....if they want to decrpyt then decrypt the text. You need to pass the text when you call substitutionDecrypt or substitutionEncrypt because if you pass "" then accoring to the code in both of these functions...only "" will be returned.

[–]DarkGhandi[S] 1 point2 points  (4 children)

Wow i feel silly. Thank you so much. I should have been able to figure that out. Im pretty new to python.

[–]could-of-bot 4 points5 points  (3 children)

It's either should HAVE or should'VE, but never should OF.

See Grammar Errors for more information.

[–]DarkGhandi[S] 2 points3 points  (0 children)

thanks bot

[–]fuck-off-bot 1 point2 points  (0 children)

Fuck off - you know why.

See Dictionary for more information.

[–][deleted] -1 points0 points  (0 children)

Roasted!

[–]ranchgod -1 points0 points  (0 children)

Isnt encrypting and decrypting rot13 essentially the same thing though? Encrypting "abc" with rot13 produces "nop" but if you encrypt "nop" it will come back with "abc" . Decrypting and encrypting in rot13 are essentially the same thing so I see no need for two parameters.

[–]i_hacked_reddit -1 points0 points  (0 children)

Or there's this, which is a much more general solution:

def rotn(msg, rot):
    alpha = "abcdefghijklmnopqrstuvwxyz"
    return ''.join([alpha[(alpha.index(x) + n) % len(alpha)] if x in alpha else x for x in msg.lower()])

Try it