[Tutor] input()

Alan Gauld alan.gauld at freenet.co.uk
Fri Apr 29 23:54:01 CEST 2005

> Matrix = input("Matrix=")
> error=input("error=")
> alpha= input("alpha=")
>   using  Macpython it works fine but when I use a terminal all I get
> a blank line.

Can you tell us a bit more about how you areusing MacPython
and the terminal?

Macpython has a console which presents a Python prompt and you type
the commands as shown. If you want to run the program at the Terminal
you have two options:

1) write the program into a text file using vi, emacs, BB EDit or
   Then run the fuile using python:

$ python myscript.py


2) type python and type the commands in at the >>> prompt.

To use the script in the way you describe below you must go with
option (1)
and create a text file with the program in.

> When I try to enter at the command line I get this
> Matrix=error=alpha=

This actually looks like you are using a script file but then just
enter when prompted. Are you actually typing any input at the Terminal
If so is it showing up anywhere?

> Also I need to redirect any output from the program into another
> which is why I used the terminal in the first place. So, I guess I
> two problems
> 1. How do I redirect output using Macpython?

Thats difficult and I wouldn't even try. Running the program script
at the Termninal is the way to go.

> 2. How do I use input() while using a terminal?

First can you start Python in command prompt mode - so you get the >>>

If so does using input() work there?

Second try a very simple program that asks the user for their name
and says hello back:

name = raw_input("What's your name? ")
print "Hello", name

[ Notice I used raw_input which is safer than input from a security
  of view, but you don;t need to worry too much about that just yet.]

Does that work as expected?

If so type it into a plain text file called hello.py
and try running it from the Terminal prompt by typing

python hello.py

Does that work?

Tell us how you get on.

Alan G
Author of the Learn to Program web tutor

More information about the Tutor mailing list