[Tutor] Newbie question: random.sample illusion?

Moedeloos Overste kloosterjunkie at hotmail.com
Thu Dec 7 22:44:41 CET 2006


Dear Luke,

Wow, thank you for all the input.You really made an effort .It's always nice 
when someone is willing to share his knowledge with you. You really opened 
my eyes on some things. I guess it's gonna be a long night for me. But I'm 
really enjoying myself though :-)

Regards,

Robert

>From: Luke Paireepinart <rabidpoobear at gmail.com>
>To: Moedeloos Overste <kloosterjunkie at hotmail.com>
>CC: tutor at python.org
>Subject: Re: [Tutor] Newbie question: random.sample illusion?
>Date: Thu, 07 Dec 2006 12:17:23 -0600
>
>Moedeloos Overste wrote:
>>Hi everybody,
>>
>>I'm in the process of learning Python and working my way through 
>>O'Reilly's "Learning Python". As an excercise I wrote (some copy/paste as 
>>well) a small lottery program just to learn how to work with lists and 
>>dictionarys etc.
>>
>>The user has to enter 6 numbers from a range(1-45). The numbers are stored 
>>in a list(num_list). Then the program asks the user how many times(vDraws) 
>>he wants to play the lottery with his numbers. After entering the program 
>>draws the lottery the desired number of times and compares the winning 
>>numbers with the users numbers.
>>
>>Now, by accident I stumbled upon the following; if the user enters the 
>>numbers 1 to 6 as his lottery numbers he always wins! So somewhere I must 
>>have made a mistake. Could somebody have a look at the code please and 
>>tell me where I took a wrong turn?
>>
>>
>>
>>winnings=0
>>right2=1
>>right3=4
>>right4=15
>>right5=450
>>right6=1000000
>>
>it might be clearer to do
>prizes = [0,0,1,4,15,450,1000000]
>>user_nums=[] # empty List for numbers
>>print "Kies je nummers tussen 0 en 46"
>>print
>>whereat=1
>>listindex=0
>>
>What is the point of having two variables here?
>You never use listindex.
>You should start whereat at 0.
>># when 7th number is inputted loop breaks
>>while whereat <= 6:
>>
>And loop until whereat is less than 6.
>That's the general consensus on looping in Computer Science.  Starting at 
>0.
>>     num=raw_input("Voer "+str(whereat)+". Lotto nummer in: ")
>>
>And just change this line to str(whereat+1) instead.
>>     # check that user inputted value is not smaller than 1 or bigger than 
>>45
>>     # and that it is not already in user_nums
>>     if num != "" and len(num) != 0:
>>
>If they enter 'a' your program will crash.
>A better solution would be to use a try.... except block.
>eg. try:
>      tmp = int(num)
>      if tmp >= 1 and tmp <= 45 and tmp not in user_nums:
>         user_nums.append(tmp)
>          whereat += 1
>except ValueError:
>    pass
>>         if int(num) >= 1 and int(num) <= 45 and num not in user_nums:
>>              user_nums.append(num)
>>
>Also note here that you're using the integer representation of num to 
>compare it to 1 and 45,
>but then you're storing the string representation of it in user_nums.
>This may be part of why you're having a problem.
>>              whereat+=1
>>              listindex+=1
>>             # if above statement if false just pass and ask for the 
>>number again!
>>         else:
>>             pass
>>print '1 trekking kost U 1 euro inzet'
>>print
>>vDraws = input("Hoe vaak wil je in de lotto spelen met deze nummers? :>")
>>
>># Create dictionary for statiscal purposes later on :-)
>>d={1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0, 10:0, 11:0, 12:0, 13:0, 
>>14:0, 15:0,
>>     16:0, 17:0, 18:0, 19:0, 20:0, 21:0, 22:0, 23:0, 24:0, 25:0, 26:0, 
>>27:0, 28:0,
>>     29:0, 30:0, 31:0, 32:0, 33:0, 34:0, 35:0, 36:0, 37:0, 38:0, 39:0, 
>>40:0, 41:0, 42:0,
>>     43:0, 44:0, 45:0}
>>
>This could be:
>d = {}
>for x in range(1,46):
>    d[x] = 0
>Or several varations of this, using lambda & map, and such.
>>match=0
>>count=0
>>
>You don't use count in your program anywhere.
>>inzet=vDraws * 1
>>
>There's absolutely no reason to multiply something by 1.
>>while vDraws > 0:
>>     x=random.sample(range(1,46), 6)# draws lottery from given range
>>     for i in x:
>>         y=int(i)
>>
>This line is unnecessary, 'i' is already an integer.
>>         d[y] = int(d[y])+ 1 #stores values in lottonumbers dictionary
>>
>Can be shortened to d[y] += 1
>>         for p in user_nums:
>>            count+=1
>>
>no reason to do this because the for loop will end when it runs out of 
>variables.
>>            y=str(y)
>>
>Again, if you leave the original items as integers you don't have to do all 
>this conversion.
>>            if p in y:
>>
>I would change this to 'if p == y', because I'm not sure what 'in' is doing 
>here.
>>                 match+=1 # increase matching variable by one if its right 
>>number
>>
>>         # caculating winnings
>>         if match ==2:
>>             winnings+=right2
>>             match=0
>>         elif match ==3:
>>             winnings+=right3
>>             match=0
>>         elif match == 4:
>>             winnings+=right4
>>             match=0
>>         elif match == 5:
>>             winnings+=right5
>>             match=0
>>         elif match == 6:
>>             winnings+=right6
>>             match=0
>>
>Using that prizes list, this can be shortened to
>winnings += prizes[match]
>match = 0
>>         vDraws = vDraws - 1
>>
>Or vDraws -= 1
>># sorting dictionary by its values
>>items=d.items()
>>backitems=[ [v[1],v[0]] for v in items]
>>backitems.sort()
>>sortedlist=[ backitems[i][1] for i in range(0,len(backitems))]
>>
>>print
>>print
>>print
>>saldo=winnings-inzet
>>print
>>print
>>print '-' * 80
>>if saldo > 0:
>>     print 'U heeft', saldo, 'euro winst gemaakt!'
>>else:
>>     print 'U heeft', saldo, ' euro verlies geleden!'
>>
>>
>>print 'U heeft in totaal gewonnen', winnings, 'euro.'
>>print 'Uw inzet was', inzet, 'euro'
>>print 'De 6 meest gevallen getallen waren:',
>>for x in sortedlist[0:6]: print x, #prints the 6 most drawn numbers
>>
>>_________________________________________________________________
>>Veilig & gerust mailen met de verbeterde antivirusscan van Live Mail! 
>>http://imagine-windowslive.com/mail/launch/default.aspx?Locale=nl-nl
>>
>>_______________________________________________
>>Tutor maillist  -  Tutor at python.org
>>http://mail.python.org/mailman/listinfo/tutor
>>
>>
>

_________________________________________________________________
Veilig & gerust mailen met de verbeterde antivirusscan van Live Mail! 
http://imagine-windowslive.com/mail/launch/default.aspx?Locale=nl-nl



More information about the Tutor mailing list