Evan Tang
Elder Moderator
| Wed Jul 01, 2020 | 04:07 PM |
Likes: 2
Categories: Tech | Pubby Cash Received:
0
Do you know what Powerball is? If you do not know what it is, it is a lottery game. The computer randomly generates a set of 5 numbers between 1 through 31, and you have to try to guess it. If you guess all the numbers right, you get a lot of money. But if you guess some of the numbers correctly, you get a fraction of the money. In this article, I will explain how to create a lottery system using python. First of all, we will make the guessing numbers 0 to 9. We will also make a set of 3 numbers. This will make it easier to guess the number. The first thing you want to do is to make a winning set of numbers. We also need to make a few guesses using a dictionary. Here is an example.
pool = {'andy':[(1,2,3),(2,3,4),(3,4,5)],'evan':[(1,7,3),(5,9,8),(4,3,7)]
win = [7,4,3]
After that, you need to make a few variables. Here is an example.
big_tickets_numb = 0 #how many grand prize tickets
big_winners = {} #who was the big winner or winners
small_tickets_numb = 0 #how many small prize tickets
small_winners = {} #who was the small winner or winners
big_pcts = {} #percentage of money the big winner gets
small_pcts = {} #percentage of money the small winner gets
After making a few variables, you need to start looping them, because you want it to check your guesses multiple times. To do this, use the a for loop. You can also make a match_count variable to see how many times your guesses matched the real answer. Here is an example.
for k in pool:
for j in range(len(pool[k])):
match_count = 0
for i in range(len(pool[k][j])):
if pool[k][j][i] in win:
match_count += 1
if match_count == 2:
small_winners[k] = small_winners.get(k,0) + 1
small_tickets_numb += 1
if match_count == 3:
big_winners[k] = big_winners.get(k,0) + 1
big_tickets_numb += 1
Then, you need to figure out how much percent each person gets if multiple people get the same winning value. To do this use an if statement followed by a for loop and then return those values. The big winner gets 80 percent of the money and the small winner get 20 percent of the money. But if two people both get the big money or the small money, then we must divide them equally. Here is an example.
if small_winners:
for k in small_winners:
small_pcts[k] = small_winners[k]*0.2/small_tickets_numb
if big_winners:
for k in big_winners:
big_pcts[k] = big_winners[k]*0.8/big_tickets_numb
return small_winners,small_pcts,big_winners,big_pcts
Finally, put all of what you just did into a function and then print it. Here is an example.
def lottery_winner(pool, win):
#Put everything you just did right here
small_winners,small_pcts,big_winners,big_pcts = lottery_winner({'Evan':[(1,2,3)]},[1,2,3])
print(small_winners,big_winners)
print(small_pcts,big_pcts)
If you did all of that, you should have a working lottery system....
Read more