Update pwrball.py - handle excpetion when avoid.cvs file does not exist.

This commit is contained in:
cpan 2022-11-03 11:34:55 -07:00
parent a5efb9a553
commit b3b7c15fc2

View File

@ -1,60 +1,64 @@
#! /usr/bin/env python3 #! /usr/bin/env python3
# Generate one random powerball lotto number that is not an avoided number in the avoid.csv file. # 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. # It simply redraw a number if the previous drawn number is an avoided number.
import secrets import secrets
import logging import logging
import csv import csv
def meganumber(): def meganumber():
picks = [] picks = []
# the mega million valid numbers are between 1 to 69 inclusive # the mega million valid numbers are between 1 to 69 inclusive
choices = list(range(1,70)) choices = list(range(1,70))
for i in range(5): for i in range(5):
logging.debug("choices are: %s", choices) logging.debug("choices are: %s", choices)
pick = secrets.choice(choices) pick = secrets.choice(choices)
choices.remove(pick) choices.remove(pick)
picks.append(pick) picks.append(pick)
logging.debug("choices left: %s", choices) logging.debug("choices left: %s", choices)
logging.debug("first five numbers: %s", picks) logging.debug("first five numbers: %s", picks)
picks.sort() picks.sort()
# the mega ball number is from 1 to 26 # the mega ball number is from 1 to 26
pick = secrets.choice(list(range(1,27))) pick = secrets.choice(list(range(1,27)))
picks.append(pick) picks.append(pick)
return picks return picks
def main(): def main():
# logging.basicConfig(level=logging.DEBUG, format = "[%(levelname)s] (%(threadName)-10s) %(message)s",) # logging.basicConfig(level=logging.DEBUG, format = "[%(levelname)s] (%(threadName)-10s) %(message)s",)
logging.basicConfig(level=logging.INFO, format = "[%(levelname)s] (%(threadName)-10s) %(message)s",) logging.basicConfig(level=logging.INFO, format = "[%(levelname)s] (%(threadName)-10s) %(message)s",)
avoid = [] avoid = []
with open("avoid.csv", "r") as f:
reader = csv.reader(f) try:
for row in reader: with open("avoid.csv", "r") as f:
introw = [] reader = csv.reader(f)
for num in row: for row in reader:
introw.append(int(num)) introw = []
avoid.append(introw) for num in row:
introw.append(int(num))
for item in avoid: avoid.append(introw)
logging.debug("before: %s", item) except FileNotFoundError:
item[0:5] = sorted(item[0:5]) pass
logging.debug("after: %s", item)
for item in avoid:
pickone = meganumber() logging.debug("before: %s", item)
while pickone in avoid: item[0:5] = sorted(item[0:5])
print("the picked number is in the avoid list: %s, regenerate", pickone) logging.debug("after: %s", item)
pickone = meganumber()
pickone = meganumber()
print(pickone) while pickone in avoid:
print("the picked number is in the avoid list: %s, regenerate", pickone)
pickone = meganumber()
if __name__ == "__main__":
main() print(pickone)
if __name__ == "__main__":
main()