Beginners Query - Simple counter problem

J Sisson sisson.j at gmail.com
Thu Sep 6 19:55:52 CEST 2007


Silly question, but are you importing random somewhere in that file?

It works on both my XP machine and my Linux machine (both from CLI) after
importing random...though it runs through this:

while count <= i:

i + 1 times...(You initialize count to 0, then loop over it until it hits
i+1 (at which point the while breaks), which means if you run d6(3), you're
actually getting back random.randint(1,6) x 4, not x3 (as would be
expected)).  Initializing count to 1 will fix that, or you could change the
while to:

while count < i:

Either way works.

Jonathon

On 9/6/07, David Barr <david.barr456 at btinternet.com> wrote:
>
> I am brand new to Python (this is my second day), and the only
> experience I have with programming was with VBA.  Anyway, I'm posting
> this to see if anyone would be kind enough to help me with this (I
> suspect, very easy to solve) query.
>
> The following code is in a file which I am running through the
> interpreter with the execfile command, yet it yeilds no results.  I
> appreciate I am obviously doing something really stupid here, but I
> can't find it.  Any help appreciated.
>
>
> def d6(i):
>      roll = 0
>      count = 0
>      while count <= i:
>          roll = roll + random.randint(1,6)
>          count += 1
>
>      return roll
>
> print d6(3)
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Computers are like air conditioners...
They quit working when you open Windows.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070906/99c1b935/attachment.html>


More information about the Python-list mailing list