Python 替换不在Encription程序中工作

Python 替换不在Encription程序中工作,python,python-3.x,Python,Python 3.x,我正在创建一个加密程序。该方案通过如下方式旋转消息中的元音来工作: “a”变成“e”,“e”变成“i”,“i”变成“o”,“o”变成“u”,“u”变成“a” 如果一个字母不是元音,它会移位n次。如果n是5,那么 “b”变成“g”,“c”变成“h”,“d”变成“i”,“x”变成“d”,“z”变成“e” e、 字母“break”变为“gwiep”。要对其进行解密,请执行反向操作。 这是我的代码,但它只针对元音,没有给我所需的输出 print('Welcome to the data encrypti

我正在创建一个加密程序。该方案通过如下方式旋转消息中的元音来工作:

“a”变成“e”,“e”变成“i”,“i”变成“o”,“o”变成“u”,“u”变成“a”

如果一个字母不是元音,它会移位n次。如果n是5,那么 “b”变成“g”,“c”变成“h”,“d”变成“i”,“x”变成“d”,“z”变成“e” e、 字母“break”变为“gwiep”。要对其进行解密,请执行反向操作。 这是我的代码,但它只针对元音,没有给我所需的输出

print('Welcome to the data encryption program.')

words = input('Enter your messsage:r ')

for char in words:
    if char == 'a':
        words = words.replace('a','e')
    elif char == 'e':
        words = words.replace('e','i')
    elif char == 'i':
        words = words.replace('i','o')
    elif char == 'o':
        words = words.replace('o','u')
    elif char == 'o':
        words = words.replace('o','u')


print(words)
这就是我得到的结果

Enter your messsage: aeiou
uuuuu

words.replace
将替换字符串中的所有字母,因此在第一次遇到
a
之后,它们将被替换为
e
。然后,每当遇到
e
时,以前替换的
a
(现在是
e
)也将被转换为
i
,等等

最好不要使用
replace()
,而是将每个翻译附加到翻译后的字符串中:

print('Welcome to the data encryption program.')

words = input('Enter your messsage: ')
translated = '';

for char in words:
    if char == 'a':
        translated += 'e'
    elif char == 'e':
        translated += 'i'
    elif char == 'i':
        translated += 'o'
    elif char == 'o':
        translated += 'u'
    else:
        translated += char

print(translated)
结果:

[bart@localhost playground]$ python3 foo.py 
Welcome to the data encryption program.
Enter your messsage: aeiou
eiouu

我还必须补充一点,这种“加密”不应该在实际应用中使用。

单词。replace
将替换字符串中的所有字母,因此在第一次遇到
a
之后,它们将被
e
替换。然后,每当遇到
e
时,以前替换的
a
(现在是
e
)也将被转换为
i
,等等

最好不要使用
replace()
,而是将每个翻译附加到翻译后的字符串中:

print('Welcome to the data encryption program.')

words = input('Enter your messsage: ')
translated = '';

for char in words:
    if char == 'a':
        translated += 'e'
    elif char == 'e':
        translated += 'i'
    elif char == 'i':
        translated += 'o'
    elif char == 'o':
        translated += 'u'
    else:
        translated += char

print(translated)
结果:

[bart@localhost playground]$ python3 foo.py 
Welcome to the data encryption program.
Enter your messsage: aeiou
eiouu

我还必须补充一点,这种“加密”不应该在实际应用中使用。

我认为最简单的方法是使用字典:

code = {"a":"e", "e":"i", "i":"o", "o":"u", "u":"a"}

word = input("Your word: ")

encoded = [code[w] if w in code else w for w in word]
encoded = ''.join(encoded)

首先,我用你的代码定义一个字典,我要一个单词并把它转换成一个列表。我替换应该被替换的元素,否则我将旧元素保留在列表中。最后,我将列表连接到一个字符串。

我认为最简单的方法是使用字典:

code = {"a":"e", "e":"i", "i":"o", "o":"u", "u":"a"}

word = input("Your word: ")

encoded = [code[w] if w in code else w for w in word]
encoded = ''.join(encoded)
首先,我用你的代码定义一个字典,我要一个单词并把它转换成一个列表。我替换应该被替换的元素,否则我将旧元素保留在列表中。最后,我将列表连接到一个字符串。

这可能会起作用:

new_word = []

for char in words:
    if char == 'a':
        new_word.append('e')
    elif char == 'e':
        new_word.append('i')
    elif char == 'i':
        new_word.append('o')
    elif char == 'o':
        new_word.append('u')
    elif char == 'u':
        new_word.append('a')


print(''.join(new_word))
这可能会奏效:

new_word = []

for char in words:
    if char == 'a':
        new_word.append('e')
    elif char == 'e':
        new_word.append('i')
    elif char == 'i':
        new_word.append('o')
    elif char == 'o':
        new_word.append('u')
    elif char == 'u':
        new_word.append('a')


print(''.join(new_word))

你的实现的问题是你把所有的角色都变形成彼此,直到它们都变成u。你的a变成了e,然后你的a和e变成了i,…,直到所有的角色都是u

保留字符并就地交换的一种方法是,首先用不同的值替换它们,然后将新字符串转换为您要查找的字符串。一个例子是将它们与大写值交换,然后简单地将字符串改为小写以获得答案

此外,这不必在循环中完成,可以在字符串上完成

print('Welcome to the data encryption program.') 

words = input('Enter your message: ')
words = words.replace('a','E')
words = words.replace('e','I')
words = words.replace('i','O')
words = words.replace('o','U')
words = words.replace('u','A')

words = words.lower()

print(words)

你的实现的问题是你把所有的角色都变形成彼此,直到它们都变成u。你的a变成了e,然后你的a和e变成了i,…,直到所有的角色都是u

保留字符并就地交换的一种方法是,首先用不同的值替换它们,然后将新字符串转换为您要查找的字符串。一个例子是将它们与大写值交换,然后简单地将字符串改为小写以获得答案

此外,这不必在循环中完成,可以在字符串上完成

print('Welcome to the data encryption program.') 

words = input('Enter your message: ')
words = words.replace('a','E')
words = words.replace('e','I')
words = words.replace('i','O')
words = words.replace('o','U')
words = words.replace('u','A')

words = words.lower()

print(words)

您的加密方案不起作用:例如,如果n=5,则
i
j
都将被编码为
o
,因此不可能解密-无论n的值是多少。在最后一节中似乎有一个错误:检查“o”并替换“o”、“u”两次。我想你的意思可能是其中一个检查'u'并在结尾处替换'u','a'(?);是的,我已经修好了。我很感激@Austinastingsi将把它们分配给一个特殊的角色@Thierrylahuille您的加密方案不起作用:例如,如果n=5,则
i
j
都将被编码为
o
,因此不可能解密-无论n的值是多少。在最后一节中似乎有一个错误:检查“o”并替换“o”、“u”两次。我想你的意思可能是其中一个检查'u'并在结尾处替换'u','a'(?);是的,我已经修好了。我很感激@Austinastingsi将把它们分配给一个特殊的角色@谢谢你,这很有效!!。该程序未在应用程序中使用。无需担心。谢谢。该程序运行良好!!。该程序没有在应用程序中使用。没有必要担心。这一点很好!有人可能会争论可读性,但我想它看起来更好,所以我编辑了它,这是一个很好的观点!人们可能会争论可读性,但我想它看起来更好,所以我编辑了它