[Tutor] stopping at 100 entries

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Sat, 7 Oct 2000 14:27:38 -0700 (PDT)


On Sat, 7 Oct 2000 CMNOLEN@aol.com wrote:

> I am working through the 'Learning to Program Python' exercises. I had no 
> trouble with the first exercise, a program to accept entries of numbers, 
> tallying and reporting the sum until 100 is met or exceeded. I am now 
> attempting to write one that will accept 100 entries, tracking the sum. I 
> cannot seem to come up with a block that will stop the calculations after the 
> 100th(or 10th or 5th etc.) entry. A point in the right direction would be 
> greatly appreciated!cmn


Hello!  I'll try to go from an indirect angle.

Imagine if we wanted to go through a list of names:

###
>>> for name in ['herimone', 'potter', 'weasley']:
...     print "Hello,", name
... 
Hello, herimone
Hello, potter
Hello, weasley
###


The for/in loop is very useful, and easy to use.  Also, Python makes it
very easy to construct a list of increasing numbers:

###
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for x in range(3):
...   print x
... 
0
1
2
###


For your problem, the trick is to construct a list of items, but ignore
the iterating variable.

###
>>> for dontcare in range(5):
...     print "hello hello"
... 
hello hello
hello hello
hello hello
hello hello
hello hello
###


Hope this helps!