[Tutor] command ...

Magnus Lycka magnus@thinkware.se
Wed Dec 11 04:25:01 2002


At 23:00 2002-12-10 -03-30, Adam Vardy wrote:
>If I type some commands, and some come after ... and at some point it
>stops and complains with a syntax error, it seems like it abandons
>what I already entered.

I'm not sure what you mean:
 >>> a = "Hello"
 >>> b = "World"
 >>> print a b
Traceback (  File "<interactive input>", line 1
     print a b
             ^
SyntaxError: invalid syntax
 >>> print a, b
Hello World

My variable definitions weren't abandoned.

If you are in a block (def, if, while etc), Python will run
it all at once, and if there is an error, you have to redo
the block, but in most of the environments (IDLE, PythonWin
and character mode interpreters with readline enabled etc)
you can do that with up-arrow etc.

Here's an example from a PythonWin session:

 >>> for name in ['Brian', 'Bicycle repair man', 'Ron Obvious']:
...     greet = 'Hello' # Stupid to have it in the loop, I know
...     print greet name # Missing comma
Traceback (  File "<interactive input>", line 3
     print greet name # Missing comma
                    ^
SyntaxError: invalid syntax
 >>>     print greet, name
Traceback (  File "<interactive input>", line 1
     print greet, name
     ^
SyntaxError: invalid syntax
 >>> # The syntax error here is due to the indentation. We "lost" the for
 >>> # loop, so the code should not be intented at all.
 >>> # I will now press up-arrow until I reach the "print greet name" line, or
 >>> # simply click there with the mouse. Then I press enter to get a copy
 >>> # of my code that I can edit the normal way. It will run when I press 
enter.
 >>> for name in ['Brian', 'Bicycle repair man', 'Ron Obvious']:
...     greet = 'Hello' # Stupid to have it in the loop, I know
...     print greet, name # Now I added a comma
...
Hello Brian
Hello Bicycle repair man
Hello Ron Obvious
 >>>

>I should be able to back up, and cross out the last command, shouldn't I?
>If it didn't like it.

Use the interactive interpreter for small tests, and by
all means as a calculator etc, but if you want to do something
"real": Put it in a file, save it, and run it non-interactively.

When python finds problems, you correct them and rerun your
program file.


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se