Advertisement
dougllio

powerballcheck2.py

Apr 27th, 2014
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.30 KB | None | 0 0
  1. # Check your powerball numbers!
  2. # created by Brandon I.
  3. # (modifications made for using lists, sets, list comprehension, reading from URL)
  4.  
  5. import sys
  6. import urllib2
  7.  
  8. # this will be how we check our numbers
  9. def checknumbers(winning_numbers, ticket, verbose=True):
  10.     prize = 0
  11.  
  12.     # checks winningness of powerball which will always be the sixth element
  13.     if winning_numbers[5] == ticket[5]:
  14.         matchpowerball = True
  15.         if verbose: print "You matched the powerball"
  16.     else:
  17.         matchpowerball = False
  18.         if verbose: print "You did not match the powerball"
  19.        
  20.     # will check numbers for matches by converting the lists (first five elements only)
  21.     # to sets and counting the number of matches in each set with an intersection
  22.     whiteballs = len(set(winning_numbers[0:5]).intersection(set(ticket[0:5])))
  23.     if verbose: print "You matched",whiteballs,"White Balls"
  24.  
  25.     # check for prizes using the number of white balls as the index of the array
  26.     powerprizes = [4,4,7,100,10000,"Jackpot"]
  27.     prizes = [0,0,0,7,100,1000000]
  28.     if matchpowerball:
  29.         return str(powerprizes[whiteballs])
  30.     else:
  31.         return str(prizes[whiteballs])
  32.  
  33. # opens winning numbers text file in same directory as script file.
  34. # format the file with a comma delimeter, for example: 19,25,29,36,48,12
  35. # the first five should be the white numbers and the last (sixth) should be the powerball
  36. input_winning_numbers = open("winningnumbers.txt", "r")
  37.  
  38.  
  39. # opens the text file containing your numbers on your ticket in the same directory as script file.
  40. # format the file with a comma delimeter as above, but a semicolon between sets of numbers on the ticket
  41. # for example: 19,25,29,36,48,12;5,6,29,35,51,21;34,39,42,44,59,8
  42. input_your_numbers = open("yournumbers.txt", "r")
  43.  
  44. # reads the text file
  45. initial_winning_numbers = input_winning_numbers.read()
  46. initial_your_numbers = input_your_numbers.read()
  47.  
  48. # closes the files correctly
  49. input_winning_numbers.close()
  50. input_your_numbers.close()
  51.  
  52. # separate the winning numbners into an integer list using list comprehension
  53. # to make sure our inputted numbers are actually integers. It's like combining
  54. # several lines of code into one. It creates a list using the for loop to loop
  55. # through the list created by split(), and it converts each item to an integer,
  56. # but only does so if the item is an integer. Pretty slick.
  57. winning_numbers = [int(num) for num in initial_winning_numbers.split(',') if num.isdigit()]
  58. print "Winning numbers:", winning_numbers
  59. print ""
  60.  
  61. # make sure we have 6 valid numbers
  62. if len(winning_numbers) <> 6:
  63.     print "There seems to be an error with the winning numbers file. Are they numbers?"
  64.     raw_input("Press Enter to Exit")
  65.     sys.exit()
  66.  
  67. # separate the tickets and numbners on each ticket into a two dimensional integer list
  68. # using list comprehension to help make sure our inputted numbers are actually integers.
  69. your_tickets = [[int(num) for num in ticket.split(",") if num.isdigit()] for ticket in initial_your_numbers.split(';')]
  70.  
  71. # make sure we have 6 valid numbers for each ticket before checking for the prize
  72. for ticketcounter,ticket in enumerate(your_tickets,1):
  73.     print "Ticket",ticketcounter,ticket
  74.     if len(ticket) <> 6:
  75.         print "There seems to be an error with ticket",ticketcounter,". Are they numbers?"
  76.         print
  77.         #raw_input("Press Enter to Exit")
  78.         #sys.exit()
  79.     else:
  80.         # this sends the numbers to my check 'function' above and returns the prize amount
  81.         ticketprize = checknumbers(winning_numbers,ticket)
  82.         print "The prize for ticket",ticketcounter, "is $"+ticketprize
  83.         print
  84.  
  85.  
  86.  
  87. #
  88. # additional
  89. #
  90. # check against old drawings by reading powerball data from URL
  91. # data is in this format:
  92. # --
  93. # Draw Date   WB1 WB2 WB3 WB4 WB5 PB  PP
  94. # 04/26/2014  30  03  22  07  33  20  3
  95. # 04/23/2014  29  48  36  19  25  12  4
  96. # 04/19/2014  35  06  51  05  29  21  5
  97. print "Checking against old drawings"
  98.  
  99. # read only 20 000 chars
  100. data = urllib2.urlopen("http://www.powerball.com/powerball/winnums-text.txt").read(20000)
  101.  
  102. # then split it into lines
  103. data = data.split("\r\n")
  104.  
  105. # remove header row
  106. data.pop(0)
  107.  
  108. # parse rows of winning numbers
  109. for line in data:
  110.     # split items using the double space delimiter
  111.     lineItems = line.split("  ")
  112.  
  113.     # pop off the date into a separate veriable
  114.     date = lineItems.pop(0)
  115.     old_numbers = [int(number.strip()) for number in lineItems if number.isdigit()]
  116.  
  117.     #check to see if we have reached the end of the data
  118.     if len(old_numbers)<6:
  119.         break
  120.  
  121.     # Loop through our tickets and and call the function to check for any prizes
  122.     # No need to throw error messages here, or show detailed information, just the prize
  123.     for ticketcounter,ticket in enumerate(your_tickets,1):
  124.         if len(ticket) == 6:
  125.             ticketprize = checknumbers(old_numbers,ticket,False)
  126.             if ticketprize.isdigit() and int(ticketprize)>0:
  127.                 print "I found a $"+ticketprize+" prize for ticket #"+str(ticketcounter)+" from " + date
  128.  
  129. print ""
  130. print "Prizes differ in California. see powerball.com for CA prize info. This program and its developer are not related in any way to Powerball or the Multistate Lottery Association (MUSL)"
  131. print ""
  132. raw_input("Press enter to exit")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement