help! simple question

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 65
Reputation: StarZ is an unknown quantity at this point 
Solved Threads: 1
StarZ StarZ is offline Offline
Junior Poster in Training

help! simple question

 
0
  #1
May 4th, 2009
the topic is writing/reading text files, but i got this simple question. how do i make this program so it will look like this: http://i39.tinypic.com/5554wi.jpg i can't figure it out.

- to make it keep looping and asking "enter your name or break to exit" continue the question until the user enters break.

try my program and be apreciated is neone can help me fill in the missing code. ty

  1. # Anna Huang
  2. # Programming Assignment 16
  3.  
  4. blank=""
  5. while not blank:
  6. y = open("pa16names.txt", "w")
  7. x = raw_input("Enter your name or break to exit:")
  8. y.writelines(x)
  9. y. close()
  10.  
  11. y = open ("pa16names.txt", "r")
  12. print "The names you've entered into the text file are:", y.read()
  13. y.close()
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: help! simple question

 
0
  #2
May 5th, 2009
Two things:

1) Separate the code that goes into the loop from code that does not loop by using proper indentations

2) Unless this is an exercise in writelines(), it is better to build a temporary string with the names entered in the loop, and then write that temporary string to the file after your break out of the loop. Otherwise you have to worry about appending a file, and if the file exists to start with an empty file.

Here is your code corrected to work properly:
  1. # Anna Huang
  2. # Programming Assignment 16
  3.  
  4. # start with empty temp string
  5. temp = ""
  6. while True:
  7. name = raw_input("Enter your name (just Enter to exit): ")
  8. if name:
  9. # build up the temp string and end the name with a space
  10. temp += name + ' ' # same as: temp = temp + name + ' '
  11. else:
  12. break
  13.  
  14. #print temp
  15.  
  16. fw = open("pa16names.txt", "w")
  17. # write the temp string to file
  18. fw.write(temp)
  19. fw.close()
  20.  
  21. fr = open ("pa16names.txt", "r")
  22. print "The names you've entered into the text file are:", fr.read()
  23. fr.close()
Please read the comments so you can learn something at least.
Last edited by sneekula; May 5th, 2009 at 11:40 am. Reason: space
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 65
Reputation: StarZ is an unknown quantity at this point 
Solved Threads: 1
StarZ StarZ is offline Offline
Junior Poster in Training

Re: help! simple question

 
0
  #3
May 5th, 2009
hey thanks, but do you know how I can make it so when the user types 'break' it will exit instead of enter to exit
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,047
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 935
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: help! simple question

 
0
  #4
May 5th, 2009
It would be something like this ...
  1. ...
  2. while True:
  3. name = raw_input("Enter your name (or break to exit): ")
  4. if name == 'break':
  5. break
  6. else:
  7. # build up the temp string and end the name with a space
  8. temp += name + ' ' # same as: temp = temp + name + ' '
  9. ...
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC