Python Forum

Full Version: letter translator (or someting)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i'm trying to make a translator (i'm not sure that's called translator) that changes every letter to it's equivalent in it's alternative version(for example a=o). i wrote something but it's not working.
Alp1 = "abcdefghıjklmnoprstuvyzxqwABCDEFGHIJKLMNOPRSTUVYZXQW"
Alp2 = "ozskadcmufvhpnebtgyijrlxwqOZSKADCMUFVHPNEBTGYİJRLXWQ"


def variable(word,Num1=0, Num2=0,):
    while Num2 != len(word) + 1:
        Num1 = 0
        Num2 += 1
        while word[Num2] != Alp1[Num1]:
            Num1 += 1
        word[Num2] = Alp2[Num1]
        return word


print(variable(input("enter the word:")))
what do i do?
Read up on python string operators. You may FIND something here

https://docs.python.org/2/library/string.html
Use this dictionary to translate
for i in range(0,len(Alp1)):
   dct[Alp1[i]]=Alp2[i]
(Apr-28-2020, 01:56 AM)deanhystad Wrote: [ -> ]Read up on python string operators. You may FIND something here

https://docs.python.org/2/library/string.html

if, as I guess, @deanhystad is pointing in direction of string.maketrans() - it was depreciated in 3.1 and removed in 3.2.
It's now static method of built-in str type. Look into it in combination with translate() method
Arrrgh! It bugs me that my link to python docs always takes me to 2.7 and this is not the first time I forgot to change to the 3.8 version. If we asked really nice do you think the internet would erase all python information prior to version 3, or in this case maybe version 3.6?