[Tutor] strategy in dealing with user input
Alan Gauld
alan.gauld at btinternet.com
Sun Sep 30 00:52:13 CEST 2007
"Robert Jackson" <wolf1boy85 at yahoo.com> wrote
>I have a few lines of code as follows:
>
> CONFIGFILE = raw_input("Enter config location: ")
> while CONFIGFILE == "":
> CONFIGFILE = raw_input("You must enter configuration file
> location: ")
All uppercasse is usually used to indicate a constant value.
Also the second line indent is wrong, so it should look like:
CONFIGFILE = raw_input("Enter config location: ")
while CONFIGFILE == "":
CONFIGFILE = raw_input("You must enter configuration file
location: ")
> Is there a way to clear out the CURRENT line while in the *while*
> loop
> so that even if the user hits enter a few extra times, it'll stay in
> the SAME line
There are various ways to do that, the best solution is to use curses,
but thats a steep lrearning curve. An easier way is just not to print
the
line inside the loop, like this:
CONFIGFILE = raw_input("Enter config location: ")
if not ConfigFile:
print "You must enter configuration file location: "
while CONFIGFILE == "":
CONFIGFILE = raw_input()
However that will still scroll the line up the screen, not so pretty.
The next simplest solution is to use sys.stdout/sys.stdin to
access the input/output buffers directly. This will allow you to
read the input directly including the newline character and to
write control codes to stdout to erase p[revious characters etc.
But this gets messy quickly.
Which takes us back to curses (assuming you are on Linux!)
or the msvcrt module if you are on Windows. Both of these
have a getch function to read characters one by one and
allows you to process them.
No easy solutions, but several ways to do it depending on
your needs and enthusiasm levels.
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list