From b3b7c15fc2ed3895eef669cf879eb99a36b903ca Mon Sep 17 00:00:00 2001 From: cpan Date: Thu, 3 Nov 2022 11:34:55 -0700 Subject: [PATCH] Update pwrball.py - handle excpetion when avoid.cvs file does not exist. --- pwrball.py | 124 +++++++++++++++++++++++++++-------------------------- 1 file changed, 64 insertions(+), 60 deletions(-) diff --git a/pwrball.py b/pwrball.py index 3526b96..5754ecc 100755 --- a/pwrball.py +++ b/pwrball.py @@ -1,60 +1,64 @@ -#! /usr/bin/env python3 - -# Generate one random powerball lotto number that is not an avoided number in the avoid.csv file. -# It simply redraw a number if the previous drawn number is an avoided number. - -import secrets -import logging -import csv - -def meganumber(): - - picks = [] - # the mega million valid numbers are between 1 to 69 inclusive - choices = list(range(1,70)) - - for i in range(5): - logging.debug("choices are: %s", choices) - pick = secrets.choice(choices) - choices.remove(pick) - picks.append(pick) - - logging.debug("choices left: %s", choices) - logging.debug("first five numbers: %s", picks) - picks.sort() - - # the mega ball number is from 1 to 26 - pick = secrets.choice(list(range(1,27))) - picks.append(pick) - - return picks - -def main(): - - # logging.basicConfig(level=logging.DEBUG, format = "[%(levelname)s] (%(threadName)-10s) %(message)s",) - logging.basicConfig(level=logging.INFO, format = "[%(levelname)s] (%(threadName)-10s) %(message)s",) - - avoid = [] - with open("avoid.csv", "r") as f: - reader = csv.reader(f) - for row in reader: - introw = [] - for num in row: - introw.append(int(num)) - avoid.append(introw) - - for item in avoid: - logging.debug("before: %s", item) - item[0:5] = sorted(item[0:5]) - logging.debug("after: %s", item) - - pickone = meganumber() - while pickone in avoid: - print("the picked number is in the avoid list: %s, regenerate", pickone) - pickone = meganumber() - - print(pickone) - - -if __name__ == "__main__": - main() +#! /usr/bin/env python3 + +# Generate one random powerball lotto number that is not an avoided number in the avoid.csv file. +# It simply redraw a number if the previous drawn number is an avoided number. + +import secrets +import logging +import csv + +def meganumber(): + + picks = [] + # the mega million valid numbers are between 1 to 69 inclusive + choices = list(range(1,70)) + + for i in range(5): + logging.debug("choices are: %s", choices) + pick = secrets.choice(choices) + choices.remove(pick) + picks.append(pick) + + logging.debug("choices left: %s", choices) + logging.debug("first five numbers: %s", picks) + picks.sort() + + # the mega ball number is from 1 to 26 + pick = secrets.choice(list(range(1,27))) + picks.append(pick) + + return picks + +def main(): + + # logging.basicConfig(level=logging.DEBUG, format = "[%(levelname)s] (%(threadName)-10s) %(message)s",) + logging.basicConfig(level=logging.INFO, format = "[%(levelname)s] (%(threadName)-10s) %(message)s",) + + avoid = [] + + try: + with open("avoid.csv", "r") as f: + reader = csv.reader(f) + for row in reader: + introw = [] + for num in row: + introw.append(int(num)) + avoid.append(introw) + except FileNotFoundError: + pass + + for item in avoid: + logging.debug("before: %s", item) + item[0:5] = sorted(item[0:5]) + logging.debug("after: %s", item) + + pickone = meganumber() + while pickone in avoid: + print("the picked number is in the avoid list: %s, regenerate", pickone) + pickone = meganumber() + + print(pickone) + + +if __name__ == "__main__": + main()