0

I made a fairly simple encode/decode program in response to a question for school, as seen below, it most likely isn't optimal but it mostly works, except that for some reason the space characters aren't just appended, they get encoded and decoded too, (space character -> comma -> colon) and I haven't been able to tell what causes it to not be picked up in the if/else.
Thank you!

def encode(string,key):
    arr=[]
    for x in string:
        if x!=" " or "," or ".":
            arr.append(ord(x))
        else:
            arr.append(x)

    count=0
    for x in arr:
        if x!=" " or "," or ".":
            if int(x)+key>90:
                arr[count]=64+((int(x)+key)-90)
                count=count+1
            else:
                arr[count]=int(x)+key
                count=count+1
        else:
            count=count+1

    for x in range(0,len(arr)):
        arr[x]=chr(arr[x])
    

    print(''.join(map(str, arr)))

    
def decode(string,key):
    arr=[]
    for x in string:
        if x!=" " or "," or ".":
            arr.append(ord(x))
        else:
            arr.append(x)

    count=0
    for x in arr:
        if x!=" " or "," or ".":
            if int(x)-key<65:
                arr[count]=90-(64-(int(x)-key))
                count=count+1
            else:
                arr[count]=int(x)-key
                count=count+1
        else:
            count=count+1
            
    for x in range(0,len(arr)):
        arr[x]=chr(arr[x])

    print(''.join(map(str, arr)))

while 1==1:
    
    string=input("enter string")
    string=string.upper()
    key=int(input("enter key"))
    switchCase=input("encode/decode?")
    if switchCase=="encode":
        encode(string,key)
    if switchCase=="decode":
        decode(string,key)
Ghost Ops
  • 1,710
  • 2
  • 13
  • 23
Lasagne
  • 11
  • 2
  • all your or need a `x!=` if you aim for simple syntax, consider making a set and using `x not in` – Abel Oct 18 '21 at 12:42
  • `if x!=" " or "," or "."` should be `if x not in ' ,.':` or similar, see linked question for why first version is incorrect – Cory Kramer Oct 18 '21 at 12:43

0 Answers0