I have been working on decode problem.
I think that my function named decode works properly.(whole codes below)
However, I do not think readline() method does not work properly.
Where should I put print(decode(line)) line??
Whole code:
import matplotlib.pyplot as plt
import pandas as pd
def decode(encoded_str):
decoded_str = ''
for char in encoded_str:
decoded_str += chr(ord(char)-1)
return decoded_str
print(decode('Hp Jsjti')) #print Go Irish
f=open('data/secret_message.txt','r')
# read and process the file one line at a time
while True:
line = f.readline()
if line =='':
break
print(decode(line))
f.close()
I tried to replace the line some times, but it did not show up anythig.
>Solution :
You are printing the line variable outside the while loop so it will return undefined and will not print anything.
Consider printing line variable within while loop:
while True:
line = f.readline()
print(decode(line)) # Decoding each line after reading it
if line === '': # Also consider `===` instead of `==` read more here: https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons
break
Hope this helps!