-1

I'm having trouble with this function. def encrypt(msg, code): '''(str, str) -> str Return msg encrypted using the given code. The code is an ordering of the alphabet plus the space and '.' characters. The position of each character in the code string gives the index of the replacement character in the regular alphabet 'abcdefghijklmnopqrstuvwxyz .'

In the first example below, 'h' is in position 20, so we 
select the letter in position 20 in the alphabet, or 'u'.

>>> encrypt('hello there', '. zyxwvutsrqponmlkjihgfedcba')
'uxqqnbiuxkx'
>>> encrypt('hello there', '. zaybxcwdveuftgshriqjpkolnm')
'rlzzyborlsl'
'''
alpha = 'abcdefghijklmnopqrstuvwxyz'
i = 0
for char in len(msg[i]):
    if msg[i] == code and msg[i] == alpha[i]:
        return msg

I have that, but it doesn't work. Can anybody help me out please?

dg123
  • 65
  • 7

2 Answers2

0

+= is the same thing as:

coded_msg = coded_msg + alpha[code.index(char)]
bfontaine
  • 18,169
  • 13
  • 73
  • 107
-1

This should do it:

def encrypt(msg, code):
    coded_msg = '' 
    for char in msg:
        coded_msg += alpha[code.index(char)]
    return coded_msg

alpha = 'abcdefghijklmnopqrstuvwxyz. '
print( encrypt('hello there', '. zyxwvutsrqponmlkjihgfedcba') )
SuperKogito
  • 2,998
  • 3
  • 16
  • 37
  • Why did you put the alpha before the def function? shouldn't it be after the docstring? – dg123 Sep 28 '17 at 01:26
  • There is no particular reason for that in this case. In my test code I put it after and it works. Just make sure for it to be seen by the encrypt function. Maybe put it in your main or in worst case in `encrypt()` itself. – SuperKogito Sep 28 '17 at 01:30
  • Also, what does the coded_msg += line mean? like what is it essentially saying? – dg123 Sep 28 '17 at 01:30
  • coded_msg is the encrypted message (output), which is a string here and I am adding the (encrypted) chars to it. You can pass the encypted chars to your msg varriable directly but that would require you to be careful about the indexes. – SuperKogito Sep 28 '17 at 01:32
  • @dg123 I added some test code, to help you understand it better. – SuperKogito Sep 28 '17 at 01:34
  • what does += mean specifically? – dg123 Sep 28 '17 at 14:03
  • @dg123 It is an incrementation of char's elements into the string. You started with `coded_msg= ''` as an empty string and now each iteration you add the extracted char from alpha to it. `coded_msg += alpha[code.index(char)]` is equal to `coded_msg = coded_msg + alpha[code.index(char)]` Check this for more: https://stackoverflow.com/questions/4435169/good-way-to-append-to-a-string – SuperKogito Sep 28 '17 at 14:21
  • Ok I got it, thanks – dg123 Sep 30 '17 at 00:36
  • Wait, it says index out of range now, when I test it – dg123 Sep 30 '17 at 01:22
  • What input throws out this error? and did you add some code? I re-tested the function and it works. Be careful not to give inputs with characters that are not included in `alpha` or in `code` such as capital letters or numbers because those are not handled by the function. – SuperKogito Sep 30 '17 at 04:22
  • Ok, I see where I might've went wrong now. It might be because I forgot to add the period and space after the alphabet. – dg123 Sep 30 '17 at 16:01
  • If the code answers your question please accept it as an answer. – SuperKogito Sep 30 '17 at 16:04
  • yes, it does, thanks – dg123 Sep 30 '17 at 16:12
  • @dg123 By accept I mean click the green tick icon on the left side of my answer :P – SuperKogito Sep 30 '17 at 16:22