[Tutor] help with script

Alan Gauld alan.gauld at btinternet.com
Wed Dec 28 10:54:29 CET 2011


On 28/12/11 01:31, nicktocco at comcast.net wrote:
> hello, my name is nick. i got python for software design by Allen B.
> Downey as a gift for christmas. i am completely new to programming

Welcome, we are here to help :-)

> 5
> x=5
> x+1
> im ok when it comes to using python so far. but it has asked me to put
> the problem into a script and run it. my question is how do i put that
> problem into a script and then how do i run it in python.

The Python interpreter interactiove prompt (>>>) does a neat trick of 
evaluating expressions and printing the result. So the 3 expressions you 
have will evaluate to 2 values:

5
6
The x assignment will not print any result.

When you put things in a script (just a text file containing python 
commands)  and run it, the interpreter will not display the result of an 
expression, you have to tell it explicitly to do that, using print().

So to make your three lines of code into a script open up any
text editor (Notepad will do for this one but you will probably
find IDLE is better longer term)

Into that file type

print (5)
x = 5
print (x+1)


Now save the file. Give it any name you like but with a .py extension.
Lets say you choose test1.py. Now if you double click that file in 
Windows explorer (I'm assuming you are on Windows, if MacOS or Linux 
suibstitute your own favourite File manager) the program will start up 
run and close down so quickly you won;t be able to read it.

To get round that add a final line:

input("Hit Enter to quit...")

Don't worry what it does just yet, just make sure its the last line in 
the file. Now, when you start the test1.py it will pause before closing 
until you hit enter.

Try that and let us know if you hit any problems.

You might also like to browse the folowing topics in my tutorial,
they all gradually introduce the idea of running Python scripts:
- Getting Started
- More Sequences
- Add a little Style


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list