[Tutor] Randrange [was: Re: Tutor Digest, Vol 92, Issue 77]

Alan Gauld alan.gauld at btinternet.com
Mon Oct 17 01:09:58 CEST 2011


On 16/10/11 21:10, Max S. wrote:
>
>
> On Sun, Oct 16, 2011 at 3:44 PM, <tutor-request at python.org
> <mailto:tutor-request at python.org>> wrote:
>
>     Send Tutor mailing list submissions to
>     tutor at python.org <mailto:tutor at python.org>
> ....
>
>     When replying, please edit your Subject line so it is more specific
>     than "Re: Contents of Tutor digest..."

Please follow the instructions.
And while you are at it please don't send the entire content of the 
digest message, edit it down to the bit that is relevant, it's much 
easier for us to read it that way, and saves bandwidth for those paying 
by the byte!

>      >
>      >import random
>      >lottery_numbers=random.randrange(1,42)
>      >print lottery_numbers
>
> In fact it is.  I notice, however, that you include 2 arguments in the
> randrange method, which only takes 1 argument.


Nope.

------------------------
 >>> help(random.randrange)
Help on method randrange in module random:

randrange(self, start, stop=None, step=1, int=<class 'int'>, 
default=None, maxwidth=9007199254740992) method of random.Random instance
     Choose a random item from range(start, stop[, step]).

     This fixes the problem with randint() which includes the
     endpoint; in Python this is usually not what you want.
     Do not supply the 'int', 'default', and 'maxwidth' arguments.
--------------------------

As you can see it takes quite a few more than one...

 > To do this, this code should work:
> times = 1
> while times < 7:
>      import random
>      lottery_numbers=random.randrange(1, 42)
>      print lottery_numbers
>      times+=1

It would be better to have the import outside the loop.
Also for a fixed number of iterations a for loop is
more common:

import random
for n in range(7):
     print random.randrange(1,42)

HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list