[Tutor] Re: input() : part two

2005-04-30 Thread Chris Smith
I found a work around for the terminal it appears that the message in
the input("message")  was being assigned to the next variable making
Matrix=error=alpha
It's good to see that you got this working.  Just a couple notes:
1) Regarding your comment above, it just *looks* like it was doing an  
assignment because of the "=" that you had for the Matrix and error  
prompt strings. If you had used the prompt "?" instead, the first line  
of the file would have been "???".  One way you could also get around  
this is being interpreted in your program is to print a "#" before  
doing any input:

###
print "#", # note the comma which keeps the output on the same line.
Matrix = input("Matrix=")
error = input("error=")
alpha = input("alpha=")
###
This would produce "# Matrix=error=alpha=" in your redirected output.
2) Also, it is possible to do a "redirect" from the IDE by just opening  
a file and then redirecting output to this file:

#--- 
--
# normal output
fav_number = input("What is your favorite number?")

# output redirected to file
import sys
file_name = 'myCode.py'
file = open(file_name, 'w') #careful; this overwrites an already  
existing file
old_stdout = sys.stdout #let's remember where we *were* sending  
output
sys.stdout = file   #now everything that gets printed will  
go the the file

print "print 'my favorite number is',",fav_number
file.close()#close the file
sys.stdout = old_stdout #restore the output
# normal output again
print "Now open and run",file_name
#--- 
--
'''--the output--
What is your favorite number?42
Now open and run myCode.py
   --end output--'''

In the file that was created there is a single line that, for this  
case, says

###
print 'my favorite number is', 42
###
If it starts to get tricky keeping track of what is being printed to  
the program, you might want to check out the string interpolation  
module that allows you to substitute in variables from your main script  
just by putting a $ before the variable name in a string (e.g.
this:
	printpl("print 'The favorite number is $fav_number'")
will make (with the input from above):
	print 'The favorite number is 42'

The module and demos are at http://lfw.org/python/Itpl.py
Best regards,
/c
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Re: input()

2005-04-29 Thread Chris Smith
On Friday, Apr 29, 2005, at 09:48 America/Chicago, 
[EMAIL PROTECTED] 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
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor