函数使用方法如下。
#将字符串转换为其ASCII数值的Python函数是ord()
ord('A')#返回65
ord('Z')#返回90
#它的逆函数为chr()
chr(65)#返回'A'
chr(90)#返回'Z'
制作一个暗号程序。
msg_lwr = input("Enter a message to encode or decode:")
msg_upr = msg_lwr.upper()
opt = ""
for ltr in msg_upr:
if ltr.isupper():
vle = ord(ltr) + 13
ltr = chr(vle)
if not ltr.isupper():
vle -= 26
ltr = chr(vle)
opt += ltr
print("Output message:",end="")
print(opt)
运行程序。
Enter a message to encode or decode:welcome to csdn!
Output message:JRYPBZR GB PFQA!