dimanche 27 juin 2021

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

I'm trying to shift every english character by 2, but leave any other character(such as !, *, ^, etc) just the way it is. However, my code keeps on shifting all characters, why?

Here's my code:

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)}')
    

Expected Output

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

Code Output

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

How can I fix this? It's strange to me how the code shifts -, +, ! despite the conditions.

Aucun commentaire:

Enregistrer un commentaire