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 user_nums=[] # empty List for numbers print "Kies je nummers tussen 0 en 46" print whereat=1 listindex=0 # when 7th number is inputted loop breaks while whereat <= 6: num=raw_input("Voer "+str(whereat)+". 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 if num != "" and len(num) != 0: if int(num) >= 1 and int(num) <= 45 and num not in user_nums: user_nums.append(num) 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} match=0 count=0 inzet=vDraws * 1 while vDraws > 0: x=random.sample(range(1,46), 6)# draws lottery from given range for i in x: y=int(i) d[y] = int(d[y])+ 1 #stores values in lottonumbers dictionary for p in user_nums: count+=1 y=str(y) if p in y: 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 vDraws = 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@python.org http://mail.python.org/mailman/listinfo/tutor