当S
为空时,它似乎正在运行,但由于我的第一个if
语句,它不应该运行
它在第15行抛出错误:只能将str
(而不是“NoneType”)连接到str
def encipher(S, n):
"""
Returns a forward shifted string based on the rules of caesar cipher
Argument S: String to be shifted
Argument n: number of spaces shifted
"""
if len(S) > 0:
if ord("a")<=ord(S[0])<=ord("z"): #lowercase letters
if ord("a") <= ord(S[0])+n <= ord("z"): #if the shifter letter is in the alphabet range add
return rot(S[0],n) + encipher(S[1:],n)
else: #else subtract
return rot(S[0],(n-26)) + encipher(S[1:],n)
elif ord("A")<=ord(S[0])<=ord("Z"): #uppercase letters
if ord("A") <= ord(S[0])+n <= ord("Z"): #if the shifter letter is in the alphabet range add
return rot(S[0],(n)) + encipher(S[1:],n)
else: #else subtract
return rot(S[0],(n-26)) + encipher(S[1:],n)
else:
return S[0] + encipher(S[1:],n)
else:
return
def rot(c, n):
#shifts a character
if len(c)>0:
return chr(ord(c)+n)
else:
return
当encypher到达字符串的最后一个字符时,它将使用空字符串调用函数'
输出:“”
它将根据您的代码返回None值。 请更改返回值:
您有
None
个返回,只要将它们更改为字符串(""
)就可以了输出:
相关问题 更多 >
编程相关推荐