A simple Decryption Program using Python
Use built in methods!

Your position_in_alphabets function can be reduced to one line. In fact, replace y = ... with the following:

alphabets.index(character) + 1


index returns the first occurrence of the character in question. Since you're working with the alphabet, it will return the position of the character in that string. Then you just need to add one.

Never trust user input

Instead of trusting the user will only enter lowercase characters after telling them to do so, call .lower() on the input to change the string to all lowercase letters.


I'm going to step through each line of the main function now.



f"" strings

I would use f"" strings so you can directly format your variables in your strings. In this case, it reduces the clutter that print("...", var, "...", var, "...") causes. It would instead be print(f"... {var} ... {var} ...").

Naming conventions (PEP 8)

a isn't really a good parameter name. A variable/parameter name should represent what that variable is holding or is doing. I would use string, since you're working with them in this program.

def decrypt(string):


Use enumerate

enumerate allows you to work through an iterable with the index and the value at that position in the iterable. It gets rid of having to iterable[i] and all that junk.

output = ''
spaces = 0
for index, character in enumerate(string):


Allowing spaces

Spaces are really easy to deal with. If the character is a space, simply add " " to the output and continue, quite literally, to the next iteration.

if character == " ":
    output += " "
    spaces += 1
    continue


Compress your computations

Instead of having a bunch of one character variable names x, y, z, I would do all the computations in one step and assign them to a variable. I've done so, and used position, because that's what you're calculating.

position = (index + 1) + (alphabet.index(character) + 1) - 26


Simplify your reassignments

Have a look at this:

if position &gt; len(alphabet):
    position %= len(alphabet)


It's the same thing as position = position % len(alphabet), but shorter (also called in place modulus). It also works for the other operators (+-*/).

Don't make variables you don't need

The sole purpose of alpha is to hold the value at the passed position in alphabet, then you add alpha to output. How about you just skip the first step and add it right to output?

output += alphabet[position - 1]




All in all, your final program looks like this:

alphabet = 'abcdefghijklmnopqrstuvwxyz'

def decrypt(string: str) -&gt; str:
    output = ''
    spaces = 0
    for index, character in enumerate(string):
        if character == " ":
            output += " "
            spaces += 1
            continue
        position = (index + 1) + (alphabet.index(character) + 1) - len(alphabet)
        if position &gt; len(alphabet):
            position %= len(alphabet)
        output += alphabet[position - 1 - spaces]
    return output

word = input("Please enter the word to be decrypted: ").lower()
output = decrypt(word)
print(f"The word '{word}' decrypted is '{output}'")


We use spaces to shift the index back for every space we've encountered.