[Tutor] powerball

Marilyn Davis marilyn at deliberate.com
Wed Jun 11 20:36:11 CEST 2008


On Wed, June 11, 2008 9:58 am, max baseman wrote:

Hi Max,

Here's your code with the indents preserved.  I'll make my comments with >'s.

from random import randrange
wins=0
win=[]
count =0
while count !=5:

>  To loop 5 times it is better to use:

>  for count in range(5):
>       stuff you want to do

>  Then all the loop controlling happens in one place and it is hard to
>  mess up.

     number=randrange(55)+1
     win.append(number)
     count=count+1
powerball=randrange(42)+1
count=0
win.sort()
while count !=146107962:
     numbers=[]
     count2=0
     while count2 !=5:
         number=randrange(55)+1

>  Yep, indeed, here you made the easy mistake of forgetting to + 1 to
>  count2.  So, it probably spun forever right there.

>  I also don't see that "count" is every changed, so that would be another
>  spin.  All these would go away by using 'for' 'in' and 'range'.

>  It looks like a fun program and a good start.

>  Marilyn Davis

     if number in win:
         numbers.append(number)
     else:
         print "lose"
         break
     numbers.sort()
     ball=randrange(42)+1
     if ball==powerball:
         print "win"
print
print
print win, powerball

> as a fun little project i scripted a powerball program. it seems to have a
> bug in it that i cant find.
>
> from random import randrange wins=0 win=[] count =0 while count !=5:
> number=randrange(55)+1 win.append(number) count=count+1
> powerball=randrange(42)+1 count=0 win.sort() while count !=146107962:
> numbers=[] count2=0 while count2 !=5: number=randrange(55)+1 if number in
> win:
> numbers.append(number) else:
> print "lose" break numbers.sort() ball=randrange(42)+1 if ball==powerball:
> print "win" print print print win, powerball
>
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list