在python2.7的字母表中,将一个数字转换成它各自的字母

2024-10-01 15:46:58 发布

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

我目前正在做一个python项目,获取一个文本字符串,通过添加关键字对文本进行加密,然后输出结果。我目前有这个程序的所有功能,除了转换回文本数值。在

例如,原始文本将转换为数值,例如[a, b, c]将变成{}。在

目前我不知道如何纠正这个问题,欢迎任何帮助,我目前的代码如下:

def encryption():
    print("You have chosen Encryption")
    outputkeyword = []
    output = []

    input = raw_input('Enter Text: ')
    input = input.lower()

    for character in input:
        number = ord(character) - 96
        output.append(number)

    input = raw_input('Enter Keyword: ')
    input = input.lower()
    for characterkeyword in input:
        numberkeyword = ord(characterkeyword) - 96
        outputkeyword.append(numberkeyword)
        first = output
        second = outputkeyword

    print("The following is for debugging only")
    print output
    print outputkeyword

    outputfinal = [x + y for x, y in zip(first, second)]
    print outputfinal

def decryption():
    print("You have chosen Decryption")
    outputkeyword = []
    output = []
    input = raw_input('Enter Text: ')
    input = input.lower()
    for character in input:
        number = ord(character) - 96
        output.append(number)

    input = raw_input('Enter Keyword: ')
    input = input.lower()
    for characterkeyword in input:
        numberkeyword = ord(characterkeyword) - 96
        outputkeyword.append(numberkeyword)
        first = output
        second = outputkeyword

    print("The following is for debuging only")
    print output
    print outputkeyword

    outputfinal = [y - x for x, y in zip(second, first)]
    print outputfinal

mode = raw_input("Encrypt 'e' or Decrypt 'd' ")

if mode == "e":
    encryption()
elif mode == "d":
    decryption()
else:
    print("Enter a valid option")
    mode = raw_input("Encrypt 'e' or Decrypt 'd' ")

    if mode == "e":
        encryption()
    elif mode == "d":
        decryption()

Tags: in文本numberforinputoutputrawmode
3条回答

假设您有一个数字列表,比如a = [1, 2, 3]和一个字母表:alpha = "abcdef"。然后将其转换如下:

def NumToStr(numbers, alpha):
    ret = ""
    for num in numbers:
        try:
            ret += alpha[num-1]
        except:
            # handle error
            pass
    return ret

根据你的问题,你想把数字转换回文本。在

可以为数字和字母表声明两个列表,例如:

数字=[1,2,3….26]

alpa=['a'、'b'、'c'…'z']

然后从num列表中找到索引/位置,并在alpa列表中找到该索引的字母表。在

虽然你的问题不太清楚,但我写了一个用字典加密的脚本

plaintext = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ') #Alphabet String for Input
Etext = list('1A2B3C4D5E6F7G8H90IJKLMNOP') """Here is string combination of numbers and alphabets you can replace it with any kinda format and your dictionary will be build with respect to the alphabet sting"""              

def messageEnc(text,plain, encryp):
  dictionary = dict(zip(plain, encryp))
  newmessage = ''
  for char in text:
    try:
        newmessage = newmessage + dictionary[char.upper()]
    except:
        newmessage += ''
 print(text,'has been encryptd to:',newmessage)

def messageDec(text,encryp, plain):
 dictionary = dict(zip(encryp,plain))
 newmessage = ''
 for char in text:
    try:

        newmessage = newmessage + dictionary[char.upper()]

    except:
        newmessage += ''
 print(text,'has been Decrypted to:',newmessage)


while True:
 print("""
   Simple Dictionary Encryption :
   Press 1 to Encrypt
   Press 2 to Decrypt
   """)
  try:
     choose = int(input())
  except:
     print("Press Either 1 or 2")
     continue

  if choose == 1:

    text = str(input("enter something: "))
    messageEnc(text,plaintext,Etext)
    continue
  else:
    text = str(input("enter something: "))
    messageDec(text,Etext,plaintext)

相关问题 更多 >

    热门问题