jeudi 15 octobre 2020

Print key and if conditional is met print key value

I would like to create simple "coding script".

I have this dictionary:

diction =  {
"A" :  "Z", 
"Y" :  "B",
"C" :  "X"
}

I want to give some random sentence, iterate through it's letters and if letter is found in this dictionary - print oposite letter
So, If I put word

"ABC"

it should return:

"ZYX"

I tried this code, but I have a "KeyError" :

# Defining dictionary
diction =  {
"A" :  "Z", 
"Y" :  "B",
"C" :  "X",
"W" :  "E",
"E" :  "V",
"U" :  "F",
"G" :  "T",
"S" :  "H",
"I" :  "R",
"Q" :  "J",
"K" :  "P",
"O" :  "L",
"M" :  "N",
" " :  " "
}

# Sentence in "szyfr" variable should be split into list.

szyfr = "SOME SENTENCE WHATEVER"

def split(szyfr): 
     return [char for char in szyfr]

szyfr = split(szyfr)


# Now I want to iterate through "szyfr" and replace letters as in "CAT" example:  

for i in szyfr:
        if i in diction:
                
                diction = {x:y for x,y in diction.items()}
                print(i)
                print("Variable: " + i + " is in 'key'")
                pass
        elif diction[i] in szyfr:
                diction = {y:x for x,y in diction.items()}
                print(i)
                print("Variable: " + i + " is in 'value'")
        elif i is " ":
                pass

print(szyfr)

Aucun commentaire:

Enregistrer un commentaire