# a Lotto simulator ... # here we let the user pick the winning numbers and the computer buys the tickets # # the user selects 6 unique random numbers from 1 to 50 and creates a list # the computer generates similar lists and compares these lists with the user list # whenever there are 3, 4, 5 or 6 matches, corresponding counters are updated # finally all the matches are shown # tested with Python24 vegaseat 14sep2006 import random def computer_random(): """let the computer create a list of 6 unique random integers from 1 to 50""" ci = random.sample(range(1,50), 6) return ci def user_random(): """let the user create a list of 6 unique random integers from 1 to 50""" print "Enter 6 unique random integers from 1 to 50:" ui = [] while len(ui) < 6: print len(ui) + 1, try: i = input("--> ") # check if i is unique and has a value from 1 to 50 # and is an integer, otherwise don't append if (i not in ui) and (1 <= i <= 50) and type(i) == type(7): ui.append(i) except: print "Enter an integer number!" return ui def match_lists(list1, list2): """to find the number of matching items in each list use sets""" set1 = set(list1) set2 = set(list2) # set3 contains all items comon to set1 and set2 set3 = set1.intersection(set2) # return number of matching items return len(set3) # the user picks the 6 winning numbers user_list = user_random() print "Winning numbers:", user_list # set up counters for 3 to 6 matches match3 = 0 match4 = 0 match5 = 0 match6 = 0 print # the computer picks the numbers for each ticket sold tickets_sold = 1000000 print "Just a moment ..." for k in range(tickets_sold): comp_list = computer_random() matches = match_lists(comp_list, user_list) if matches == 3: match3 += 1 elif matches == 4: match4 += 1 elif matches == 5: match5 += 1 elif matches == 6: match6 += 1 # optional progress indicator if k % 100000 == 0: print ">", print; print print "Out of %d tickets sold the computer found these matches:" % tickets_sold print "3 matches = %d" % match3 print "4 matches = %d" % match4 print "5 matches = %d" % match5 print "6 matches = %d" % match6
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question