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)