I am trying to make a kind of a pronounceable cryptographic language in python. I want to iterate through a string and change i.e. just some vowels.
i thought this could be done with an array chars["a","o"]where the first letter should be replaced with the second one.
I am experimenting with this code.
import string
string = "Hello, my name is fesker"
chars =[
["a","o"],
["e","i"],
["i","u"],
["o","e"],
["u","a"],
["H","J"],
]
for i in range(len(string)):
print(string[i])
replaced=False
for x in range(len(chars)):
if string[i] in chars[x][0] and replaced == False:
string = string.replace(string[i],chars[x][1])
replaced=True
print("Replace",chars[x][0],"with",chars[x][1])
print(string)
I dont get the point, i think the function should be correct but string replace gives me a different output. The final sentence should be read as "Jille, my nomi us fosker"
but the python shell gives me that "Jelle, my neme es fesker":
H
Replace H with J
e
Replace e with i
l
l
o
Replace o with e
,
m
y
n
a
Replace a with o
m
i
Replace i with u
u
Replace u with a
s
f
a
Replace a with o
s
k
o
Replace o with e
r
Jelle, my neme es fesker
What am i doing wrong?
string = string.replace(string[i],chars[x][1])<- this replaces ALL instances of the letter selected. So, in the string"Hello", the letter'e'goes in this rotation'e' -> 'i' -> 'u' -> 'a' -> 'o' -> 'e', so it appears to be unchanged, but it is actually been changed 5 times.charsa translation table; you want to translate "a"->"o", "e"->"i", etc. So you want to do all the character translating at the same time, to avoid the issue you get.stringpackage you just imported by overwriting it withstring = "Hello, my name is fesker", call thatssor something instead.