Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8332357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:41:56+00:00 2026-06-09T02:41:56+00:00

I have less than a year of programming experience. While learning about reading and

  • 0

I have less than a year of programming experience. While learning about reading and writing files I came across this tutorial: 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's create a file and write it to disk.
filename = "test.dat"
# Let's 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 a Python 2.7.3 Shell. I am prompted repeatedly to enter strings which are appended to a list that will be written to a file (this makes sense to me). What I don’t understand is the condition for exiting the While loop (“While not done:”). I thought this meant that I type done at the prompt to exit the loop and subsequently write the file, but done has no effect. Then I thought that any non-string entered at the prompt should break the loop and write the file. I couldn’t get the loop to break at all; for anything I entered at the prompt, I was just prompted again with “Enter a name:”.

By removing the While loop and retaining the if/else statement, I got the code to work for a single prompt. Can someone tell me what I am not understanding here? I am guessing it is a fairly simple concept that wasn’t explained in the tutorial because it was assumed to be obvious. Since “done” is such a common word, I wasn’t able to find any Python specific meanings for it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:41:58+00:00Added an answer on June 9, 2026 at 2:41 am

    done is assigned once on line 3:

    done = 0
    

    Therefore, this the while loop will continue to loop as long as done is still “not 0”:

    while not done:
    

    I.e. it will continue to loop forever, unless it hits a break statement (line 11). Unfortunately, the code is flawed and that will never happen.

    If you want to stop when you type ‘done’, then change the if statement to:

    if name == "done":
    

    But, be aware that the literal string done above has nothing to do with the variable done assigned earlier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched for this, and i found that for api less than 12,
Hopefully this is less complex than I think. I have one table of companies,
For about a year I have been thinking about writing a program that writes
I'm still quite new to python with less than a year of experience, and
I have been developing C++ for less than a year, but in that time,
I have sets of strings in a database. Each set will have less than
I have a collection with a relatively big index (but less than ram available)
I have less experience with Java Swing and I want to create a dynamic
I have a similar question to this one However, I have a less strict
I have this quite long query that should give me some information about shipments,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.