I am trying to write a function "decode" which takes key, filename, and indicator parameters. key is to indicates the character that needs to replace in the file. filename indicates the name of the file, and indicator indicates whether to encode or decode. Now with the code, I wrote below, no value was passing into filecontent when it loops the second time. What might be the issue here?
def decode(key, filename, indicator):
key = list(key)
openfile = open(filename,'rt')
if indicator == 'd':
if len(key) % 2 > 0:
return
else:
for i in range(0, len(key), 2):
key[i] = key[i].lower()
key[i+1] = key[i+1].lower()
indiOne = key[i+1]
indiTwo = key[i]
filecontent = openfile.read().replace(indiOne, indiTwo)
print(filecontent)
return ''
if __name__ == '__main__':
print(decode('VFSC', 'decript.txt', 'd'))
here is the content in decript.txt:
I lofe eating cpanish apples.
and it was meant to replace all the F with V and C with S:
I love eating spanish apples.