I have just completed a main module that converts english text to pig latin. My prof included the readlines method, but I did not include it and I am confused as to why mine still works. Doesn't a file always have to be read?
Here is my solution:
import pig_latin
def main():
"""Execution begins here."""
try:
with open("antony_speech.txt", encoding="utf-8") as f_in:
try:
with open("antony_speech_pig_latin.txt", mode="w", encoding="utf-8") as f_out:
for line in f_in: # Complete the following for each line
eng_txt = line.rstrip()
f_out.write(pig_latin.english_to_pig_latin(eng_txt) + "\n")
except:
print(f"Error: File could not be created.")
except:
print(f"Error: File could not be opened.")
main()
My prof's solution:
import solution_1 # My Pig Latin module
def main():
"""Execution begins here."""
in_file = "antony_speech.txt"
out_file = "antony_speech_pig_latin.txt"
try:
with open(in_file, encoding="utf-8") as f_in:
try:
with open(out_file, mode="w", encoding="utf-8") as f_out:
eng_txt = f_in.readline().rstrip()
while eng_txt != "": # Complete the following for each line
f_out.write(solution_1.english_to_pig_latin(eng_txt) + "\n")
eng_txt = f_in.readline().rstrip()
except:
print(f"Error: File '{out_file}' could not be created.")
except:
print(f"Error: Can't open '{in_file}'.")
main()