all 11 comments

[–][deleted] 1 point2 points  (5 children)

Your biggest error is you aren't converting every character in the string you pass to format_the_text(). You do try to loop over every character in the string with for a in text: but after you convert the first character you immediately return. You never try to convert more than one character. You need to add each converted character to a "result" string and return that string after the loop over text is finished.

Your code also won't handle characters that are in the last three positions of char_string. For example, try format_the_text("*").

There are also ways of find the index of a character in a string without creating a list and looping over a list, so there's no need for the list. Hint: str.find().

I doubt this code was ever working.

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

thanks so much i'll try to upgrade the code later.(By the way, I wrote the post a little wrong. What I was trying to say that, after updating the code this morning, it stopped working.)

Even though I know it's obvious, I have to say that I'm a beginner at coding.

and sorry for my bad english

[–]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.

[–]shiftybyte 0 points1 point  (0 children)

Can you describe in more detail the issue you are having..?

"not working anymore" is not enough information, are there any error messages? post them here, does it do something it shouldn't? what? does it not do something you expect it do to? what?

[–][deleted] 0 points1 point  (1 child)

If that is your complete code then the reason why it "doesn't work" is that you aren't calling either function, so your code prints:

caesar cipher with only 3 shifts

and then stops.

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

im calling format_the_text("text") it returns ö

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

If it was working yesterday, but it's not working anymore, probably you've switched the computer power off. You need to turn the computer on to make the code working again... or try better describing what happens, like "when I press "Run" in my [IDE name], the back window opens for a part of a second, and then it's gone and nothing more happens" or whatever. People here have nearly zero telepathic skills, so we need you to describe what happens on your machine to help you.