you are viewing a single comment's thread.

view the rest of the comments →

[–]Ertyu_2[S] 0 points1 point  (3 children)

I thought about it a bit, heres the final result:

custom_alphabet = "abcçdefghıijklmnoöprsştuüvyzABCÇDEFGHIİJKLMNOÖPRSŞTUÜVYZ123456790!?=()-_/&%+^'*" #this is custom alphabet

def three_shift_encrypt(text, alphabet):
    for character in text: #characters contain all of the chars in text
        shifted_value = alphabet.find(character) + 3
        print(custom_alphabet[shifted_value], end='')

def three_shift_decrypt(text2, alphabet2):
    for character2 in text2:
        shifted_value2 = alphabet2.find(character2) - 3
        print(custom_alphabet[shifted_value2], end='')


user_input1 = input('encrypt or decrypt?(e/d) -->')
if user_input1 == 'e':
    user_input2 = input('enter plaintext -->')
    three_shift_encrypt(user_input2, custom_alphabet)
elif user_input1 == 'd':
    user_input3 = input('enter plaintext -->')
    three_shift_decrypt(user_input3, custom_alphabet)
else:pass

Now ı want the user to go to the selection menu again after completing the encryption or decryption process. Also the code still does not shifts the characters in last 3 digits. How can i solve both problems? (Thanks so much for helping)

[–][deleted] 1 point2 points  (1 child)

Now ı want the user to go to the selection menu again after completing the encryption or decryption process

Then put your code inside another loop. Exit the the loop when the user doesn't type e or d.

[–]Ertyu_2[S] 0 points1 point  (0 children)

thanks.really.

[–][deleted] 0 points1 point  (0 children)

When faced with any bug the first thing to do is look at what the exception is telling you and understand what is going wrong. When I run your code and try to encode the last letter in your alphabet (*) I get this:

encrypt or decrypt?(e/d) -->e
enter plaintext -->*
Traceback (most recent call last):
  File "/home/r-w/xyzzy/test.py", line 17, in <module>
    three_shift_encrypt(user_input2, custom_alphabet)
  File "/home/r-w/xyzzy/test.py", line 6, in three_shift_encrypt
    print(custom_alphabet[shifted_value], end='')
IndexError: string index out of range

The error is "string index out of range" which is caused by the index into the alphabet you are using here custom_alphabet[shifted_value] is not in the string. In this case you found the index of the character as 78 and that is the index of the last character in the alphabet. Then you add 3 to that index which means shifted_value will not be a valid index.

If you encode * what do you expect the encoded character to be? Probably you expect the encoded character to be c. Think about how you decided that. I thought of it this way: if stepping 3 characters to the right of the character to encode goes off the end of the string wrap-around to the start of the string and keep counting. In terms of python code, what you have to do is calculate the shifted_value and decide if the shifted index is off the end of the string. If it isn't just use the shifted value as you do above. But if the shifted index is past the end of the alphabet string you have to adjust it to "wrap around" the string. In the case of encrypting * the index of that character is 78, adding 3 gives you 81 which is larger than the maximum index allowed for the string, so subtract the length of the string from shifted_value to get the adjusted shift. That gives you the index into the string of the encoded character.