Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

readline() method does not work properly in my code

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading