text crypter program dosent give right results

Question:

when you crypt a text it works but when you try to decrypt in it gives weird result. My code was working but it suddenly broke. The things is I don’t know if encrypting part or decrypting part is broken I would appreciate any help.

import random

abc = 'a b c d e f g h i j k l m n o p q r s t u v w x y z'
code = '! @ # $ % ^ & * ( ) _ + § ± , . / < > ? | ` ~ [ ] {'
code2 = '{ ] [ ~ ` | ? > < / . , ± § + _ ) ( * & ^ % $ # @ !'

while True:
    print()
    print('--------------')
    print('[1] text -> coden[2] code -> text')
    print('--------------')
    inp = input('==> ')

# encrypting chosen
    if inp == '1':
        print('--------------')
        print('enter the text')
        print('--------------')
        inp = input('==> ')
        print('-----code-----')
        # for every character in the text given this runs
        for i in inp:
            # gets a random number between 1 and 2 because there is two different alphabets
            # every letter will be encrypted depending on a random alphabet
            rand = random.randint(1, 2)
            # gets the index for the character in abc
            ind = abc.index(i)
            if rand == 1:
                # prints the index in code2(1th alphabet) and adds '∑' so program know it belongs to 1th alphabet
                print(code2[ind], '∑', sep='', end='')
            elif rand == 2:
                # prints the index in code(@th alphabet) and adds '¥' so program know it belongs to 1th alphabet
                print(code[ind], '¥', sep='', end='')
# decrypting chosen
    elif inp == '2':
        print()
        print('--------------')
        print('enter the code')
        print('--------------')
        inp = input('==> ')
        print('-----text-----')
        # for every character in the text given this runs
        for i in inp:
            # checks the character next to the actual character if it is '∑'
            # to know which alphabet it belongs to
            if inp[inp.index(i)+1] == '∑':
                # gets the index and print the same index in abc
                ind = code2.index(i)
                print(abc[ind], end='')

            elif inp[inp.index(i)+1] == '¥':
                ind = code.index(i)
                print(abc[ind], end='')

    else:
        pass
Asked By: NoFallToggled

||

Answers:

This does what you want. Note that I add your alphabet shift BEFORE the encoded character. That way, when iterating through the encoded text, I can just set the correct decoding alphabet.

import random

abc  ='abcdefghijklmnopqrstuvwxyz'
code ='!@#$%^&*()_+§±,./<>?|`~[]{'
code2='{][~`|?></.,±§+_)(*&^%$#@!'

while True:
    print('n--------------')
    print('[1] text -> coden[2] code -> text')
    print('--------------')
    inp = input('==> ')

# encrypting chosen
    if inp == '1':
        print('--------------')
        print('enter the text')
        print('--------------')
        inp = input('==> ')
        print('-----code-----')
        # for every character in the text given this runs
        for i in inp:
            # gets a random number between 1 and 2 because there is two different alphabets
            # every letter will be encrypted depending on a random alphabet
            rand = random.randint(1, 2)
            # gets the index for the character in abc
            ind = abc.index(i)
            if rand == 1:
                # prints the index in code2(1th alphabet) and adds '∑' so program know it belongs to 1th alphabet
                print('∑', code2[ind], sep='', end='')
            elif rand == 2:
                # prints the index in code(@th alphabet) and adds '¥' so program know it belongs to 1th alphabet
                print('¥', code[ind], sep='', end='')
# decrypting chosen
    elif inp == '2':
        print()
        print('--------------')
        print('enter the code')
        print('--------------')
        inp = input('==> ')
        print('-----text-----')
        # for every character in the text given this runs
        which = code
        for i in inp:
            if i == '∑':
                which = code2
            elif i == '¥':
                which = code
            else:
                ind = which.index(i)
                print(abc[ind], end='')

A better design would have an encode function and a decode function that return strings, thereby allowing the caller to decide what to DO with the strings.

Answered By: Tim Roberts
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.