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? > > Ooops, I accidentally hit 'send' on that other e-mail before I was done with it.
I was just going to say that you forgot to include 'random'. Also, here is the code with the changes I suggested. I ran it on 1,2,3,4,5,6 for 80 tries and didn't win every time. #code begins here. import random winnings=0 prizes = [0,0,1,4,15,450,1000000] user_nums=[] # empty List for numbers print "Kies je nummers tussen 0 en 46" print whereat=0 # when 6th number is inputted loop breaks while whereat < 6: num=raw_input("Voer "+str(whereat+1)+". Lotto nummer in: ") # check that user inputted value is not smaller than 1 or bigger than 45 # and that it is not already in user_nums 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 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 = {} for x in range(1,46): d[x] = 0 match=0 count=0 inzet=vDraws while vDraws > 0: x=random.sample(range(1,46), 6)# draws lottery from given range for i in x: d[i] += 1 #stores values in lottonumbers dictionary for p in user_nums: if p == i: match+=1 # increase matching variable by one if its right number winnings += prizes[match] match = 0 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 '\n\n' saldo=winnings-inzet print '\n' 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 _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor