2016-10-29 37 views
-2

我正在写一个加密程序在python中,我需要改变输入字符串ex:HELLO和输出:LIPPS。当我输入更多的单词时,我遇到问题。加密程序错误

def encr_ypt(s, n): 
    word=ord(s) 
    for i in range(len(s)): 
     if word >= 90 and word <= 97: 
      hsl = chr(63+n) 
     if word >= 122: 
      hsl = chr(95+n) 
     else: 
      hsl = chr(word+n) 
    return hsl 

st=raw_input('input string : ') 
print encr_ypt(st, 4) 

这里是错误消息

input string : HELLO 
Traceback (most recent call last): 
    File "encrypt.py", line 13, in <module> 
    print encr_ypt(st, 4) 
    File "encrypt.py", line 2, in encr_ypt 
    word=ord(s) 
TypeError: ord() expected a character, but string of length 5 found 

回答

0

ord(i)温控功能需要一个单个字符,然后返回结果字符的ASCII值。你的代码试图获得一个字符串的ASCII值,这会返回一个错误。

此外,由于您正在使用hsl变量来存储加密的字符串,请务必使用+=运算符,它将结果字符添加到字符串中。

以下是更正代码

def encr_ypt(s, n): 
    hsl = '' 
    for word in s: 
     word = ord(word) 
     if word >= 90 and word <= 97: 
      hsl += chr(63+n) 
     if word >= 122: 
      hsl += chr(95+n) 
     else: 
      hsl += chr(word+n) 
    return hsl 

st=raw_input('input string : ') 
print encr_ypt(st, 4) 
+0

什么最好的方法我写“U'V'W'Z”和输出是性格?/^%,我希望输出回到'A' –