如何移动英文字符而不是 python 中的任何其他字符

how to shift english characters but NOT any other character in python

我试图将每个英文字符移动 2,但保留任何其他字符(如 !、*、^ 等)原样。但是,我的代码一直在移动所有字符,为什么?

这是我的代码:

sample = 'abcdefghijklmn-op+qr!stuvwxyz'

shift = 2

result = []

for i in range(len(sample)):
    print(f'ascii code of {sample[i]} is: {ord(sample[i])}')

print('#######################################')
ascii_code = 97

for j in range(len(sample)):
    if 96 < ord(sample[i]) < 123:
        ascii_code = ord(sample[j])
        ascii_code += shift
        if ascii_code > 122:
            ascii_code = ascii_code - 25 - 1
        
        print(f'Converted value: {chr(ascii_code)}')
    

预期输出

ascii code of a is: 97
ascii code of b is: 98
ascii code of c is: 99
ascii code of d is: 100
ascii code of e is: 101
ascii code of f is: 102
ascii code of g is: 103
ascii code of h is: 104
ascii code of i is: 105
ascii code of j is: 106
ascii code of k is: 107
ascii code of l is: 108
ascii code of m is: 109
ascii code of n is: 110
ascii code of - is: 45
ascii code of o is: 111
ascii code of p is: 112
ascii code of + is: 43
ascii code of q is: 113
ascii code of r is: 114
ascii code of ! is: 33
ascii code of s is: 115
ascii code of t is: 116
ascii code of u is: 117
ascii code of v is: 118
ascii code of w is: 119
ascii code of x is: 120
ascii code of y is: 121
ascii code of z is: 122
#######################################
Converted value: c
Converted value: d
Converted value: e
Converted value: f
Converted value: g
Converted value: h
Converted value: i
Converted value: j
Converted value: k
Converted value: l
Converted value: m
Converted value: n
Converted value: o
Converted value: p
Converted value: -
Converted value: q
Converted value: r
Converted value: +
Converted value: s
Converted value: t
Converted value: !
Converted value: u
Converted value: v
Converted value: w
Converted value: x
Converted value: y
Converted value: z
Converted value: a
Converted value: b

代码输出

ascii code of a is: 97
ascii code of b is: 98
ascii code of c is: 99
ascii code of d is: 100
ascii code of e is: 101
ascii code of f is: 102
ascii code of g is: 103
ascii code of h is: 104
ascii code of i is: 105
ascii code of j is: 106
ascii code of k is: 107
ascii code of l is: 108
ascii code of m is: 109
ascii code of n is: 110
ascii code of - is: 45
ascii code of o is: 111
ascii code of p is: 112
ascii code of + is: 43
ascii code of q is: 113
ascii code of r is: 114
ascii code of ! is: 33
ascii code of s is: 115
ascii code of t is: 116
ascii code of u is: 117
ascii code of v is: 118
ascii code of w is: 119
ascii code of x is: 120
ascii code of y is: 121
ascii code of z is: 122
#######################################
Converted value: c
Converted value: d
Converted value: e
Converted value: f
Converted value: g
Converted value: h
Converted value: i
Converted value: j
Converted value: k
Converted value: l
Converted value: m
Converted value: n
Converted value: o
Converted value: p
Converted value: /
Converted value: q
Converted value: r
Converted value: -
Converted value: s
Converted value: t
Converted value: #
Converted value: u
Converted value: v
Converted value: w
Converted value: x
Converted value: y
Converted value: z
Converted value: a
Converted value: b

我该如何解决这个问题?我很奇怪代码如何移动 -, +, !尽管有条件。

你的条件应该是这样的:

sample = 'abcdefghijklmn-op+qr!stuvwxyz'

shift = 2

result = []

for i in range(len(sample)):
    print(f'ascii code of {sample[i]} is: {ord(sample[i])}')

print('#######################################')
ascii_code = 97

for j in range(len(sample)):
    ascii_code = ord(sample[j])
    if 64 < ascii_code < 91 or 96 < ascii_code < 123:
        ascii_code += shift
    if ascii_code > 122:
        ascii_code = ascii_code - 25 - 1
      
    print(f'Converted value: {chr(ascii_code)}')

您不应递增任何字符的值,除非它是英文字符 - 其标识范围为 64 到 91(对于大写字母),或在 96 到 123 的范围内(对于小写字母)字母)。