# [Tutor] How to run this block of code dozens of times

Dave Angel d at davea.name
Mon Sep 17 04:22:01 CEST 2012

```On 09/16/2012 08:56 PM, Scurvy Scott wrote:
> On Sun, Sep 16, 2012 at 5:23 PM, Dave Angel <d at davea.name> wrote:
>
>> On 09/16/2012 07:56 PM, Scurvy Scott wrote:
>>> scratch that, new code is below for your perusal:
>>>
>>> from Crypto.PublicKey import RSA
>>> import hashlib
>>>
>>> def repeat_a_lot():
>>>     count = 0
>>>     while count < 20:
>>> You're kidding, aren't you?  while loops are meant for those times when
>>> you don't know how many times the loop is to iterate.
>>
> Acutally, Dave, I would be the one setting just how many times the loop
> would run manually. Actually the loop would run 2^80 times, so it was more
> of a test to see if the output was different each time it ran more than
> trying to run it the correct amount of times.
>

Since you know the count before you start the loop, then use xrange(),
rather than while.  If you had to test some condition other than a
simple count, then you might want to use while.  As the loop was coded,
you have three uses of a variable that doesn't matter to anyone reading
the code.  one to initialize it, one to test it, and one to increment it.

>>>         m = RSA.generate(1024)
>>>         b = hashlib.sha1()
>>>         b.update(str(m))
>>>         a = b.hexdigest()
>>>         print a[:16] + '.onion'
>>>         count += 1
>>> repeat_a_lot()
>>>
>>>
>>>
>> def repeat_a_lot():
>>     for _ in xrange(20):
>>         m = RSA.generate(1024)
>>         b = hashlib.sha1()
>>         b.update(str(m))
>>         a = b.hexdigest()
>>         print a[:16] + '.onion'
>>
>> repeat_a_lot()
>>
>>
> Why would you use an underscore rather than a letter or name like I've
> always seen. I've never seen an underscore used before

Standard convention for an unused loop variable.  But if you prefer, no
harm in naming it something like
for  unused_counter_here in xrange(20):

The unadorned underscore leads the reader to expect that you don't use the loop variable anywhere, like for indexing some matrix.

--

DaveA

```