Can i make this code smaller?

is there a way to make this code smaller? i tried to find anything that could help but i only found “if true a elif b elif c…” but that makes it bigger to it’s sides

print("PLEASE DONT USE ACCENTS")
while True:
    c=input(""""if you want to encrypt type 1
if you want to translate type 2"
1/2: """)
    a=input(": ")
    a=a.lower()
    if c=="1":
        b=""
        for x in a: 
            if x=="a": b+="q"
            if x=="b": b+="w"
            if x=="c": b+="e"
            if x=="d": b+="r"
            if x=="e": b+="t"
            if x=="f": b+="y"
            if x=="g": b+="u"
            if x=="h": b+="i"
            if x=="i": b+="o"
            if x=="j": b+="p"
            if x=="k": b+="a"
            if x=="l": b+="s"
            if x=="m": b+="d"
            if x=="n": b+="f"
            if x=="o": b+="g"
            if x=="p": b+="h"
            if x=="q": b+="j"
            if x=="r": b+="k"
            if x=="s": b+="l"
            if x=="t": b+="z"
            if x=="u": b+="x"
            if x=="v": b+="c"
            if x=="w": b+="v"
            if x=="x": b+="b"
            if x=="y": b+="n"
            if x=="z": b+="m"
            if x==" ": b+=""
        print(b)
    if c=="2":
        b=""
        for x in a: 
            if x=="q": b+="a"
            if x=="w": b+="b"
            if x=="e": b+="c"
            if x=="r": b+="d"
            if x=="t": b+="e"
            if x=="y": b+="f"
            if x=="u": b+="g"
            if x=="i": b+="h"
            if x=="o": b+="i"
            if x=="p": b+="j"
            if x=="a": b+="k"
            if x=="s": b+="l"
            if x=="d": b+="m"
            if x=="f": b+="n"
            if x=="g": b+="o"
            if x=="h": b+="p"
            if x=="j": b+="q"
            if x=="k": b+="r"
            if x=="l": b+="s"
            if x=="z": b+="t"
            if x=="x": b+="u"
            if x=="c": b+="v"
            if x=="v": b+="w"
            if x=="b": b+="x"
            if x=="n": b+="y"
            if x=="m": b+="z"
        print(b)

Hi !

I think the tool you’re looking for is a dictionary. It will allow you to map each key to its replacement value:

conversion_dict = {'q': 'a', 'w': 'b', ...}
...
for x in a:
    b += conversion_dict[x]
...

For building the dictionary, you still have to write each letter and its replacement explicitly, so your entire Python file will still be quite verbose. But the interesting part that actually performs the “encryption” will be much more concise and readable. Also, you’ll be able to reuse the dictionary later in the code if needed, without having to re-type everything.

1 Like

i needed this for another code that i was making, so you hit two birds with one stone. thanks!

there a translate method in str

from string import ascii_lowercase

a = "hello world"
qwerty = "qwertyuiopasdfghjklzxcvbnm"
encode_table = str.maketrans(ascii_lowercase, qwerty)
decode_table = str.maketrans(qwerty, ascii_lowercase)

b = a.translate(encode_table)
c = b.translate(decode_table)

print(a)
print(b)
print(c)
3 Likes

Wow nice, quite convenient indeed ! I learnt something today :blush:

I see that your original code collapses " " into nothing:

            if x==" ": b+=""

Is that intentional? The translate table method will just copy the " " to the output string. The equivalent of:

            if x==" ": b+=" "

if needed the maketrans method accept a third parameter string that set each character to None in the table

to_delete = " "
encode_table = str.maketrans(ascii_lowercase, qwerty, to_delete)