First post in here :)
So what I'm trying to achieve is, if a character is found inside a list in 'key' list then add the 'other' character in that list to the variable 's'. If a character is not found inside a list, then add the character itself to the variable 's'.
message="hello !"
s=""
found=False
key=[['m', 'c'], ['u', 'e'], ['b', 'g'], ['a', 'k'], ['s', 'v'], ['h', 'x'],['i', 'z'], ['r', 'y'], ['p', 'w'], ['l', 'n'], ['o', 'j'], ['t', 'f'], ['q', 'd']]
for i in message:
for j in key:
if i==j[0]:
found=True
s+=j[1]
break
elif i==j[1]:
found=True
s+=j[0]
break
if not found:
s+=i
return s
input:
hello !
expected output:
xunnj !
Somehow the later part(add the character itself to the 's' variable) does not work unless I put an else block to the first if statement. like this,
if i==j[0]:
found=True
s+=j[1]
break
elif i==j[1]:
found=True
s+=j[0]
break
else:
found=False
output without the else block:
xunnj
I want to know why,
if not found:
s+=i
does not work without an else block to the first if statement. Thanks.
Aucun commentaire:
Enregistrer un commentaire