[Tutor] Re: input()

Chris Smith smichr at bigfoot.com
Sat Apr 30 00:39:09 CEST 2005


On Friday, Apr 29, 2005, at 09:48 America/Chicago, 
tutor-request at python.org wrote:

> Hello and  thanks in advance.
> 	I am trying to prompt the user for some input. I need three values
> from the user so,I used input() like so;
> Matrix = input("Matrix=")
> error=input("error=")
> alpha= input("alpha=")
>   using  Macpython it works fine but when I use a terminal all I get is
> a blank line. When I try to enter at the command line I get this
> Matrix=error=alpha=
> Also I need to redirect any output from the program into another file,
> which is why I used the terminal in the first place. So, I guess I have
> two problems
> 	1. How do I redirect output using Macpython?
> 	2. How do I use input() while using a terminal?
>

It seems to work fine here for me, but I'm not sure what you are trying 
to do.  I'm using Python 2.4 under OS 10.2.8.  Perhaps you could paste 
a copy of the terminal prompts as they appear?  Here is what my session 
looked like when I entered 12 and 4 for two inputs:

###
csmith% cat go.py
a=input('a=')
b=input('b=')
print a,b
csmith% python go.py
a=12
b=4
12 4
###

If I make a file and feed it to the program rather than entering the 
text by hand I get:

###
csmith% cat > dat
12
4
^C
csmith% python go.py < dat
a=b=12 4
###

If I try to redirect this to a file I get:

###
csmith% python go.py < dat > out
csmith% cat out
a=b=12 4
###

One thing you might try is to change your prompt from something like 
"a=" to "a=\n"

Which of the above scenarios are you trying to work with and what do 
you want the result to be? It looks like the input to input() is not 
echoed so you should do that yourself if you want the values the user 
entered to be displayed in the file/output. e.g.

###
csmith% cat go2.py
def echoInput(prompt=""):
   ret = input(prompt)
   print ret
   return ret
a=echoInput('a=')
b=echoInput('b=')
print a,b
csmith% python go2.py < dat > out
csmith% cat out
a=12
b=4
12 4
###

/c



More information about the Tutor mailing list