擅长:python、mysql、java
<p>在移位之前,应该调用方法<a href="https://docs.python.org/2/library/stdtypes.html#str.isalpha" rel="nofollow">str.alpha</a>,以确保所选元素是字母表</p>
<pre><code>for i in range(0,len(s)):
if elem.isaplha():
s[i]=chr(ord(s[i]) + int(shift))
</code></pre>
<p>不过,你在这里做了很多工作。为什么不使用理解表达</p>
<pre><code>s = ''.join(chr(ord(elem) + shift) if elem.isalpha() else elem for elem in s)
</code></pre>
<p>或者如果你够冒险的话</p>
<pre><code>s = ''.join([elem, chr(ord(elem) + shift)][elem.isalpha()] for elem in s)
</code></pre>
<p>最后你检查了<a href="https://docs.python.org/2/library/string.html#string.maketrans" rel="nofollow">string.makestrans</a>和<a href="https://docs.python.org/2/library/stdtypes.html#str.translate" rel="nofollow">str.translate</a>来进行转换吗</p>
<pre><code>from string import maketrans, ascii_alpha
s = s.translate(maketrans(ascii_alpha[shift:] + string.ascii_alpha[:shift])
</code></pre>