[Tutor] new and freezing problem

Kalle Svensson kalle@lysator.liu.se
Sun May 25 21:37:02 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[Jennifer Cianciolo]
> Hi!
> 
> I'm new to this list.  I have no programming experience, but I'm
> trying to teach myself some with python.

Welcome!  Good choise, programming can be a lot of fun!

> I've written a short program that keeps making my computer freeze.
> Actually it's not the whole computer, just python.  This is the
> program:

When this happens, there's probably an infinite loop (a loop where the
loop condition is always true) somewhere.

> def freqChange(p, N, w11, w12, w22):
>     q=1-p
>     pnew=(N*(p**2*w11+q*p*w12))/(N*(p**2*w11+2*p*q*w12+q**2*w22))
>     while 0<=pnew<=1.0:
>         p=pnew
>         q=1-pnew

The loop condition here is 0<=pnew<=1.0, which depends on the value of
pnew.  The problem here is that you don't assign a new, changed value
to pnew in the loop body.  pnew keeps it's initial value through each
repetition, and if the condition was true in the beginning, it will be
forever.

Try running the function with values that result in a pnew smaller
than 0 or larger than 1.0.  It should return immediately.

Peace,
  Kalle
- -- 
Kalle Svensson, http://www.juckapan.org/~kalle/
Student, root and saint in the Church of Emacs.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.6 <http://mailcrypt.sourceforge.net/>

iD8DBQE+0W+BdNeA1787sd0RAtYxAJ9HpOoanUzfDLH1RIcE6Dxiv4qkNQCffmJU
F0TvSytd/YR7qS1uJWi+fqU=
=3knu
-----END PGP SIGNATURE-----