[Tutor] Newbie Anxiety

Chris F.A. Johnson cfaj at freeshell.org
Fri Nov 11 00:07:22 CET 2005


On Thu, 10 Nov 2005, Terry Kemmerer wrote:

> I'm working on Ch. 5, "Fruitful Functions", or "How To Think Like A
> Computer Scientist" and I still can't count.
> (Don't laugh! I can't play the violin either...)
>
> In Basic, I would have said:
>
> 10  x = x + 1 :  print x : goto 10

    Good heavens! I never used GOTO, even on the C-64:

10 for n = 1 to 10
20 print n
25 rem n = 1 rem  remove first rem for infinite loop
30 next


> run
> 1
> 2
> 3
> etc....
>
> I don't know why, but this great UNKNOWN bothers me a lot. Maybe it is
> because it was the first thing I learned in Basic,
> back 20 years ago when I was actually a programmer....  But I  think it
> goes toward visualizing how things are
> going flow and be constructed without line numbers and the venerable
> GOTO statement.
>
> How is this done in Python? (So I can stop holding my breath as I study
> this great language....and relax.)

n = 1
while n <= 10:
   print n
   n = n + 1


    Or:

for n in [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]:
   print n


    Or:

for n in range(10):
   print n + 1


    Etc......


-- 
     Chris F.A. Johnson                     <http://cfaj.freeshell.org>
     ==================================================================
     Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
     <http://www.torfree.net/~chris/books/cfaj/ssr.html>


More information about the Tutor mailing list