[Tutor] Newbie Anxiety

Kent Johnson kent37 at tds.net
Thu Nov 10 23:49:54 CET 2005


Terry Kemmerer wrote:
> In Basic, I would have said:
> 
> 10  x = x + 1 :  print x : goto 10
> 
> run
> 1
> 2
> 3
> etc....
> 

> How is this done in Python? (So I can stop holding my breath as I study 
> this great language....and relax.)

In Python there is no goto, as you have discovered. Loops are constructed using the 'for' and 'while' statements.

A while loop will loop as long as its condition is true. If you give it a condition that is always true, it will create an infinite loop like yours above. So the equivalent Python code would be

x=0
while True:
  x = x + 1
  print x

You can read a little more about while loops here and in the next chapter of your book:
http://docs.python.org/tut/node5.html#SECTION005200000000000000000

Now relax and forget all the bad habits from you BASIC days ;-)

Kent

-- 
http://www.kentsjohnson.com



More information about the Tutor mailing list