0

Found a bug in Python course. Got a pass despite incorrect output!

In the Python section 48.2: "Writing Files". Expected output: John Oscar Jacob My output: JohnOscarJacob However, my output was marked as correct! My code names = ["John", "Oscar", "Jacob"] file = open("names.txt", "w") #write down the names into the file for n in names: file.write(str(n)) file.close() file= open("names.txt", "r") #output the content of file in console for line in file: print(line) file.close()

6th Dec 2022, 7:08 PM
Savager
1 Odpowiedź
+ 5
Savager , to get each item in a separate line in the file, we need to add a *new-line sequence* ('\n') at the end of each line. when reading the file content, each line contains now the *new-line sequence*, that has to be removed before printing. otherwise we get an empty line between each printed output.
6th Dec 2022, 7:22 PM
Lothar
Lothar - avatar