我试着把每个英文字符都移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我怎么才能解决这个问题?对我来说很奇怪,代码如何改变-,+,!不管条件如何。
发布于 2021-06-27 11:27:47
你的条件应该是这样的:
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 (用于小写字母)。
https://stackoverflow.com/questions/68150367
复制相似问题