[Tutor] first steps

Alan Gauld alan.gauld at btinternet.com
Wed Mar 16 09:28:37 CET 2011

"Ryan McAdam" <ryan at quantodesign.com> wrote 

> I saved this module to my desktop

You probably need to create a Folder for your Python 
code or you will soon have a very busy desktop! :-)

> # File: chaos.py
> # A simple program illustrating chaotic behavior.
> def main():
>     print("This program illustrates a chaotic function")
>     x = eval(input("Enter a number between 0 and 1: "))
>     for i in range(10):
>         x = 3.9 * x * (1 - x)
>         print(x)
> main()

Don;t use eval() here it is bad practice since it opens a 
security hole in your program and its best to get into 
good security habits from the start.

Instead use the explicit type that you want the data to 
be, in your case a floating point number(float() ) 

     x = float(input("Enter a number between 0 and 1: "))

> When I open and run this module in IDLE's shell 

Thats your mistake.
Don't open files in the shell use the File->Open menu 
to create a new IDLE window that doesn't have the >>> 
prompt. Then when you run the file the output will appear 
in the IDLE shell window.

> going to Run > Check Module and then running the module. 

You shouldn't need to Check Module first (unless IDLE 
on Mac works differently to IDLE on Windows, which it shouldn't...)

> Also, when it works correctly, IDLE won't run the program 
> again via the >>> chaos.main() statement. I get this:
>     chaos.main()
> NameError: name 'chaos' is not defined

This tells you that Python does not recognise chaos.

If you do want to run from the >>> prompt (eg to test chaos) 
then you will need to import chaos first

>>> import chaos
>>> chaos.main()

HTH and welcome to the tutor list.

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list