Instructions
Requirements and Specifications
Source Code
import random
def pick_winning_numbers(lb: int, ub:int, n: int) -> list:
"""
This function returns a list of 4 non-repeated random numbers
:param lb: integer: lower bound of the range (inclusive)
:param ub: integer: upper bound of the range (inclusive)
:param n: integer: represents the number of random values to be returned
:return: list of 4 integers
"""
numbers = random.sample(range(lb, ub+1), n)
return numbers
def pick_superball(lb: int, ub: int) -> int:
"""
This function returns a random integer between the range defined by the variables 'ub' and 'lb'
:param lb: integer: lower bound of the range (inclusive)
:param ub: integer: upper bound of the range (inclusive)
:return:
"""
return random.randint(lb, ub)
def convert_list_to_integers(str_list: list) -> list:
"""
This function accepts a list of string representing valid integers, and returns a list
of these integers
:param str_list: list of strings
:return: list of integers
"""
list_int = [int(x) for x in str_list]
return list_int
def count_matches(winning_numbers: list, numbers: list) -> int:
"""
This function accepts two lists of integers, and count how many numbers from the second list
are in the first list
:param winning_numbers: list of integers
:param numbers: list of integers
:return: number of matched integers (int)
"""
matches = 0
for n in numbers:
if n in winning_numbers:
matches += 1
return matches
def main():
"""
Main function where all the functionality of the program is contained
:return: None
"""
# First, pick today's winning numbers
winning_numbers = pick_winning_numbers(1, 9, 4)
# Pick the superball number
sb = pick_superball(1, 9)
# Create helper variables to count the number of entries, number of entries that used superball,
# number of entries that matched 3 or 4 numbers, etc
n_entries = 0
n_superball_used = 0
n_matched_3 = 0
n_matched_4 = 0
# Now, read the file of entries
with open("entries.txt", 'r') as f:
# Read all lines
lines = f.readlines()
# Pick customer numbers
for i, line in enumerate(lines):
# trim and strip line
line = line.strip()
# Split line by space to convert it into a list
customer_numbers_str = line.split(" ")
# Check if this customer used superball
user_used_sb = False
if "sb" in customer_numbers_str:
# Remove the 'sb' from customer's numbers
user_used_sb = True
del customer_numbers_str[customer_numbers_str.index("sb")]
# Increment the counter for number of superball used
n_superball_used += 1
# Now, convert customer's numbers from string to int
customer_numbers = convert_list_to_integers(customer_numbers_str)
# Now, count matches
n = count_matches(winning_numbers, customer_numbers)
"""
Now, if the user used the 'sb' option:
* If the 'sb' number is in today's winning numbers, then add one additional match
to this user
* If 'sb' is not in today's winning numbers, then check if the user has this number
"""
if sb in winning_numbers:
n += 1
else:
if sb in customer_numbers and not sb in winning_numbers:
n += 1
# Increment the entries counter
n_entries += 1
# Check if the number of matches is 3 or 4
if n == 3:
n_matched_3 += 1
elif n == 4:
n_matched_4 += 1
# Now, display the results
print("Pythonic Pick 4 Lottery Results")
print("Today's winning numbers: [", end="")
for i, x in enumerate(winning_numbers):
print(x, end="")
if i < len(winning_numbers) -1:
print(", ", end="")
print("]")
print(f"Today's superball: {sb}")
print(f"Number of entries: {n_entries}")
print(f"Number of entries that used superball: {n_superball_used}")
print(f"Number of entries that matched 3 numbers: {n_matched_3}")
print(f"Number of entries that matched 4 numbers: {n_matched_4}")
if __name__ == '__main__':
main()