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
# 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()