Writing to a file using Python - '' Not yet done: "Confusing Me

I have less than a year of programming experience. Studying reading and writing files, I came across this lesson: http://www.penzilla.net/tutorials/python/fileio/

The tutorial offers the following example as a simple script to create and write to a file:

# Let create a file and write it to disk. filename = "test.dat" # Let create some data: done = 0 namelist = [] while not done: name = raw_input("Enter a name:") if type(name) == type(""): namelist.append(name) else: break # Create a file object: # in "write" mode FILE = open(filename,"w") # Write all the lines at once: FILE.writelines(namelist) # Alternatively write them one by one: for name in namelist: FILE.write(name) FILE.close() 

I copied this code and ran it through the Python 2.7.3 shell. I am repeatedly asked to enter lines that are added to the list that will be written to the file (this makes sense to me). What I do not understand is the condition for exiting the While loop ("Not yet done:"). I thought this meant that I was typing in the prompt to exit the loop and subsequently write the file, but this has no effect. Then I thought that any line entered at the prompt should break the loop and write the file. I could not make the loop break at all; for anything that I entered in the prompt, I was again offered to "Enter a name:".

By removing the While loop and saving the if / else statement, I got the code to work in a single prompt. Can someone tell me what i don't understand here? I suppose this is a fairly simple concept that was not explained in the textbook because it was supposed to be obvious. Since "done" is such a common word, I could not find any Python-specific meanings for it.

+6
source share
4 answers

done is assigned once on line 3:

 done = 0 

Therefore, this while loop will continue the loop until done is still not β€œ0”:

 while not done: 

those. it will continue the loop forever, unless it accesses the break statement (line 11). Unfortunately, the code is corrupted, and this will never happen.

If you want to stop when you type "done", change the if statement to:

 if name == "done": 

But remember that the line with the letters done above has nothing to do with the previously assigned done variable.

+2
source

I would stop following this tutorial right now. The code is not Pythonic, it is too complex, and it looks rather outdated.

So, here is how I write this tutorial code (yes, it does the same, but only correctly):

 with open('test.dat', 'w') as handle: while True: name = raw_input('Enter a name: ') if not name: break handle.write(name + '\n') 
+3
source

It is not your fault. This code provides no way to exit the loop.

 if name == 'end': break 
+2
source

The code is bad, first.

In this case, done is the name of the variable. As written, it will loop forever, since there is no way out.

You should stop following this guide and choose the best one: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

+1
source

Source: https://habr.com/ru/post/922181/


All Articles