Reputation: 164
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 opposite 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)
Upvotes: 0
Views: 63
Reputation: 12212
Following the code you gave, I find the following oddities:
szyfr = "SOME SENTENCE WHATEVER"
def split(szyfr):
return [char for char in szyfr]
szyfr = split(szyfr)
It seems you're trying to build a list from a character string, which can be simply done as:
>>> s = "hola"
>>> l1 = list(s)
>>> l1
['h', 'o', 'l', 'a']
So, in your specific case:
szyfr = "SOME SENTENCE WHATEVER"
szyfr = list(szyfr)
Though, it is not really needed, since you can directly manage a string as if it were a list, with a for.
Now, you want to replace the chars following an specific dictionary. I find your solution too complex, while you only need:
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_to_code = input("Give me a sentence: ").strip().upper()
toret = ""
for ch in sentence_to_code:
coded_ch = diction.get(ch)
if not coded_ch:
coded_ch = ch
toret += coded_ch
print(toret)
If you are not defining correspondents to all possible characters, then it is sensible to use the get(k) method of the dictionary, which returns None when the key k is not found.
It must be taken into account that the get(k) method has default parameter for the return value in case the key is not found, so you can use get(k, default_return_value), which lets us simplify the code even more:
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_to_code = input("Give me a sentence: ").strip().upper()
toret = "".join([diction.get(ch, ch) for ch in sentence_to_code])
print(toret)
Now we are using a list comprehension, since we don't need a conditional any more. The call diction.get(ch, ch)
returns ch or the corresponding coded char, or ch itself if it is not found in the dictionary. With the call to str.join(), i.e., "".join(...)
we transform the list back in a string.
Upvotes: 0
Reputation: 803
If you really wanted to use a dict in which for each key 'letter' has the value 'opposite letter' :
This is a possible solution :
diction = {" ": " "}
all_letters = range(ord('A'), ord('Z')+1)
for char, opsite_char in zip(all_letters, reversed(all_letters)):
diction[chr(char)] = chr(opsite_char)
print(diction)
OUTPUTS :
{' ': ' ', 'A': 'Z', 'B': 'Y', 'C': 'X', 'D': 'W', 'E': 'V', 'F': 'U', 'G': 'T',
'H': 'S', 'I': 'R', 'J': 'Q', 'K': 'P', 'L': 'O', 'M': 'N', 'N': 'M', 'O': 'L',
'P': 'K', 'Q': 'J', 'R': 'I', 'S': 'H', 'T': 'G', 'U': 'F', 'V': 'E', 'W': 'D',
'X': 'C', 'Y': 'B', 'Z': 'A'}
Upvotes: 0
Reputation: 59184
You are missing some letters, such as N
. Note that {"M": "N"}
is not the same thing as {"N": "M"}
.
That being said, you don't even need a dictionary as if you subtract the ASCII code of an uppercase letter (such as 65 for A) from 155 (65+65+26-1) you will end up with the ASCII code of the corresponding letter:
>>> szyfr = "SOME SENTENCE WHATEVER"
>>> "".join(chr(155-ord(e)) if "A" <= e <= "Z" else e for e in szyfr)
'HLNV HVMGVMXV DSZGVEVI'
Upvotes: 1