如何在编码句子时保留空格和特殊字符

发布于 2025-01-16 16:34:16 字数 486 浏览 3 评论 0原文

所以我正在尝试制作一个编码/解码程序,让用户输入一个句子并通过用其他字母替换字母将其转换为编码消息。

它适用于一个单词,但当我添加更多带有空格和特殊字符的单词时,它似乎不起作用。

所以这是我的代码:

 phrase = input("Write a sentence:")
decalage = int(input("By how many letters (1 to replace a with b...): "))

maping = {}
for i in range(26):
    i_cesar = (i + decalage) % 26
    c_cesar = chr(i_cesar + ord('a'))
    c = chr(i + ord('a'))
    maping[c] = c_cesar

result = ""
for c in phrase:
   result = result + maping[c]
print(result)

So I'm trying to make an encoding/decoding program, that lets the user to type a sentence and turn it into an encoding message by replacing letters with other letters.

It works fine with one words, but when I put more words with spaces and special characters it seems to be not working.

So here is my code:

 phrase = input("Write a sentence:")
decalage = int(input("By how many letters (1 to replace a with b...): "))

maping = {}
for i in range(26):
    i_cesar = (i + decalage) % 26
    c_cesar = chr(i_cesar + ord('a'))
    c = chr(i + ord('a'))
    maping[c] = c_cesar

result = ""
for c in phrase:
   result = result + maping[c]
print(result)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜是你 2025-01-23 16:34:16

您只是为小写字母构建映射。您可以做的一件事是创建一个包含您想要更改的所有字符的字符串,然后旋转该字符串的副本以形成您的字典。

from string import ascii_lowercase, digits, punctuation

characters = ascii_lowercase + digits + punctuation

def rotate(s: str, degree: int) -> str:
    return s[degree:] + s[:degree]

def cesar_encode(plaintext: str, cipher: int) -> str:
    mapping = dict(zip(characters, rotate(characters, cipher)))
    return "".join([mapping.get(c, c) for c in plaintext])

cesar_encode("abcde12345$%^&", 1)
# "bcdef23456%&_'"

You're only building a mapping for the lowercase letters. One thing you can do is make a string of all the characters you intend to change, then rotate a copy of that string to form your dictionary.

from string import ascii_lowercase, digits, punctuation

characters = ascii_lowercase + digits + punctuation

def rotate(s: str, degree: int) -> str:
    return s[degree:] + s[:degree]

def cesar_encode(plaintext: str, cipher: int) -> str:
    mapping = dict(zip(characters, rotate(characters, cipher)))
    return "".join([mapping.get(c, c) for c in plaintext])

cesar_encode("abcde12345$%^&", 1)
# "bcdef23456%&_'"
静待花开 2025-01-23 16:34:16

如果我正确理解您的要求,我认为整个事情可以简化为仅添加到字符的数字表示形式,但仅限于字符是字母的情况。

像下面这样的事情会更容易一些:

phrase = input("Write a sentence:")
decalage = int(input("By how many letters (1 to replace a with b...): "))

result = ""
for c in list(phrase): 
    if ord(c) >= 65 and ord(c) <= 122:
        result = result + chr(ord(c)+decalage) 
    else: 
        result + c

print(result)

If I understand your requirements correctly, I think this whole thing can be simplified to just add to the numeric representation of the character, but only in the case if the character is a letter.

Something like the following would be a little easier:

phrase = input("Write a sentence:")
decalage = int(input("By how many letters (1 to replace a with b...): "))

result = ""
for c in list(phrase): 
    if ord(c) >= 65 and ord(c) <= 122:
        result = result + chr(ord(c)+decalage) 
    else: 
        result + c

print(result)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文