创建Caeser程序:将ASCII转换为字符

2024-09-30 01:28:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究python,试图制作一个Caeser密码程序。 所以我做了一个GUI平台,能够让密码部分正常工作,但它只能以ASCII格式输出消息。你知道吗

当你运行我的程序时,它会获取信息,你说你想让字母表移动的字母数,然后用ASCII表示信息,我怎么才能让这部分以字母形式出现呢?你知道吗

我尝试将for循环存储到一个变量中,然后将该变量添加到一个常见的ascii-->;字符转换器中,但这不起作用。你知道吗

这是我的密码:

def encode(userPhrase):
    msg = input('Enter your message: ')
    key = eval(input("enter a number"))
    finalmsg = msg.upper()
    for ch in finalmsg:
        print( str( ord(ch)+key ), end=' ')

Tags: key程序消息密码forinput格式字母
2条回答

你需要让字母表末尾的字母环绕到A,B,C。。。您可以使用模运算(复杂)来完成,或者参见下面的示例

使用chr而不是str。传递一个参数userPhrase,然后要求输入一条消息。另外,我建议使用int而不是eval。你知道吗

def encode(userPhrase):
  msg = input('Enter your message: ')
  key = int(input("enter a number"))
  finalmsg = msg.upper()
  for ch in finalmsg:
    new_ch = ord(ch)+key
    if new_ch > ord('Z'):
      new_ch -= 26
    print( chr(new_ch), end=' ')

最后一个问题是非字母(例如空格等)

str更改为chr

print( chr( ord(ch)+key ), end=' ')

根据chr上的文档:

Return the string representing a character whose Unicode code point is the integer i. For example, chr(97) returns the string 'a', while chr(957) returns the string 'ν'. This is the inverse of ord().

相关问题 更多 >

    热门问题