Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Check your powerball numbers!
- # created by Brandon I.
- # (modifications made for using lists, sets, list comprehension, reading from URL)
- import sys
- import urllib2
- # this will be how we check our numbers
- def checknumbers(winning_numbers, ticket, verbose=True):
- prize = 0
- # checks winningness of powerball which will always be the sixth element
- if winning_numbers[5] == ticket[5]:
- matchpowerball = True
- if verbose: print "You matched the powerball"
- else:
- matchpowerball = False
- if verbose: print "You did not match the powerball"
- # will check numbers for matches by converting the lists (first five elements only)
- # to sets and counting the number of matches in each set with an intersection
- whiteballs = len(set(winning_numbers[0:5]).intersection(set(ticket[0:5])))
- if verbose: print "You matched",whiteballs,"White Balls"
- # check for prizes using the number of white balls as the index of the array
- powerprizes = [4,4,7,100,10000,"Jackpot"]
- prizes = [0,0,0,7,100,1000000]
- if matchpowerball:
- return str(powerprizes[whiteballs])
- else:
- return str(prizes[whiteballs])
- # opens winning numbers text file in same directory as script file.
- # format the file with a comma delimeter, for example: 19,25,29,36,48,12
- # the first five should be the white numbers and the last (sixth) should be the powerball
- input_winning_numbers = open("winningnumbers.txt", "r")
- # opens the text file containing your numbers on your ticket in the same directory as script file.
- # format the file with a comma delimeter as above, but a semicolon between sets of numbers on the ticket
- # for example: 19,25,29,36,48,12;5,6,29,35,51,21;34,39,42,44,59,8
- input_your_numbers = open("yournumbers.txt", "r")
- # reads the text file
- initial_winning_numbers = input_winning_numbers.read()
- initial_your_numbers = input_your_numbers.read()
- # closes the files correctly
- input_winning_numbers.close()
- input_your_numbers.close()
- # separate the winning numbners into an integer list using list comprehension
- # to make sure our inputted numbers are actually integers. It's like combining
- # several lines of code into one. It creates a list using the for loop to loop
- # through the list created by split(), and it converts each item to an integer,
- # but only does so if the item is an integer. Pretty slick.
- winning_numbers = [int(num) for num in initial_winning_numbers.split(',') if num.isdigit()]
- print "Winning numbers:", winning_numbers
- print ""
- # make sure we have 6 valid numbers
- if len(winning_numbers) <> 6:
- print "There seems to be an error with the winning numbers file. Are they numbers?"
- raw_input("Press Enter to Exit")
- sys.exit()
- # separate the tickets and numbners on each ticket into a two dimensional integer list
- # using list comprehension to help make sure our inputted numbers are actually integers.
- your_tickets = [[int(num) for num in ticket.split(",") if num.isdigit()] for ticket in initial_your_numbers.split(';')]
- # make sure we have 6 valid numbers for each ticket before checking for the prize
- for ticketcounter,ticket in enumerate(your_tickets,1):
- print "Ticket",ticketcounter,ticket
- if len(ticket) <> 6:
- print "There seems to be an error with ticket",ticketcounter,". Are they numbers?"
- print
- #raw_input("Press Enter to Exit")
- #sys.exit()
- else:
- # this sends the numbers to my check 'function' above and returns the prize amount
- ticketprize = checknumbers(winning_numbers,ticket)
- print "The prize for ticket",ticketcounter, "is $"+ticketprize
- print
- #
- # additional
- #
- # check against old drawings by reading powerball data from URL
- # data is in this format:
- # --
- # Draw Date WB1 WB2 WB3 WB4 WB5 PB PP
- # 04/26/2014 30 03 22 07 33 20 3
- # 04/23/2014 29 48 36 19 25 12 4
- # 04/19/2014 35 06 51 05 29 21 5
- print "Checking against old drawings"
- # read only 20 000 chars
- data = urllib2.urlopen("http://www.powerball.com/powerball/winnums-text.txt").read(20000)
- # then split it into lines
- data = data.split("\r\n")
- # remove header row
- data.pop(0)
- # parse rows of winning numbers
- for line in data:
- # split items using the double space delimiter
- lineItems = line.split(" ")
- # pop off the date into a separate veriable
- date = lineItems.pop(0)
- old_numbers = [int(number.strip()) for number in lineItems if number.isdigit()]
- #check to see if we have reached the end of the data
- if len(old_numbers)<6:
- break
- # Loop through our tickets and and call the function to check for any prizes
- # No need to throw error messages here, or show detailed information, just the prize
- for ticketcounter,ticket in enumerate(your_tickets,1):
- if len(ticket) == 6:
- ticketprize = checknumbers(old_numbers,ticket,False)
- if ticketprize.isdigit() and int(ticketprize)>0:
- print "I found a $"+ticketprize+" prize for ticket #"+str(ticketcounter)+" from " + date
- print ""
- 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)"
- print ""
- raw_input("Press enter to exit")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement