我在尝试做一个密码程序。
当我运行这个程序并插入,例如使用shift 2的abc时,它将返回cde,这是很好的。但是我尝试用shift 3插入xyz,而不是正确地转换abc,而是返回aaa。如果我使用shift 2,然后返回zaa,也会发生这种情况。
当使用ASCII表完成字母表时,如何调整我的程序以正确地从一开始就开始呢?
shift = int(input("Please insert a number you want to shift the characters with: "))
end = ""
for x in alf:
ascii = ord(x)
if ascii >= 97 and ascii <= 122:
res = ascii + shift
if res > 122:
res = 0 + 97
min = res + shift
end = end + chr(min)
print (end) 发布于 2016-10-03 09:03:40
这是因为你的逻辑表达错误。下面是一个示例,它将允许任何正整数作为右移位,从再次开始。它可以进行非常优化(提示:使用模运算符),但这是与您的代码和数字大声疾呼的微小变化。
for x in alf:
ascii = ord(x)
if ascii >= 97 and ascii <= 122:
res = ascii + shift
while res > 122:
res = res - (122 - 97) - 1
end = end + chr(res)https://stackoverflow.com/questions/39827719
复制相似问题