| | |
help! simple question
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 65
Reputation:
Solved Threads: 1
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
- 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
Python Syntax (Toggle Plain Text)
# Anna Huang # Programming Assignment 16 blank="" while not blank: y = open("pa16names.txt", "w") x = raw_input("Enter your name or break to exit:") y.writelines(x) y. close() y = open ("pa16names.txt", "r") print "The names you've entered into the text file are:", y.read() y.close()
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:
Please read the comments so you can learn something at least.
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:
python Syntax (Toggle Plain Text)
# Anna Huang # Programming Assignment 16 # start with empty temp string temp = "" while True: name = raw_input("Enter your name (just Enter to exit): ") if name: # build up the temp string and end the name with a space temp += name + ' ' # same as: temp = temp + name + ' ' else: break #print temp fw = open("pa16names.txt", "w") # write the temp string to file fw.write(temp) fw.close() fr = open ("pa16names.txt", "r") print "The names you've entered into the text file are:", fr.read() fr.close()
Last edited by sneekula; May 5th, 2009 at 11:40 am. Reason: space
No one died when Clinton lied.
It would be something like this ...
python Syntax (Toggle Plain Text)
... while True: name = raw_input("Enter your name (or break to exit): ") if name == 'break': break else: # build up the temp string and end the name with a space temp += name + ' ' # same as: temp = temp + name + ' ' ...
May 'the Google' be with you!
![]() |
Similar Threads
- HI/ I have a simple question (Domains and DNS)
- A simple question about Ram (Motherboards, CPUs and RAM)
- simple question (C++)
- This has to be a simple question to answer. (Visual Basic 4 / 5 / 6)
- Simple question (Windows Servers and IIS)
- Simple Question (Linux Servers and Apache)
- A simple question about CMOS batteries (Motherboards, CPUs and RAM)
- XP Pro (re)activation question (Windows NT / 2000 / XP)
Other Threads in the Python Forum
- Previous Thread: perfect numbers
- Next Thread: validation logic
| Thread Tools | Search this Thread |
alarm assignment avogadro beginner bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory dynamic error examples exe file float format function generator getvalue gnu graphics gui halp homework http ideas import input itunes java leftmouse line linux list lists logging loop maintain maze millimeter module mouse number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh stdout string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 variable ventrilo verify vigenere web webservice wikipedia windows wxpython xlib






