将字母转换为数字现在一遍又一遍地转换为三个字母

时间:2016-01-16 16:09:37

标签: python python-3.x

我有这段代码

letters = input('Write Text: ')
letters = letters.lower()
numberEn = []
car=["C","A","R"]
carEn=[]
for character in letters:
    number = ord(character) - 96
    numberEn.append(number)

for number in numberEn:
    En = (numberEn)
    En = (numberEn(car))
    print (En)

我试图做的是改变 嗨到[8,9]确实有效,然后将其改为例如

hi
[8,9]
ca/[c,a]

不起作用我也尝试将其改为字符串

1 个答案:

答案 0 :(得分:0)

BASE = ord('a') - 1

def str_to_nums(s):
    return [ord(ch) - BASE for ch in s]

def nums_to_str(nums):
    return "".join(chr(n + BASE) for n in nums)

print(str_to_nums("hi"))    # => [8, 9]
print(nums_to_str([8, 9]))  # => "hi"

print(str_to_nums("ca"))    # => [3, 1]
print(nums_to_str([3, 1]))  # => "ca"