So I build a tiny bingo program for my wife to use at her candle parties. The boards are already generated. This is just a program to call out the numbers.
import random
a = []
a = list(range(1,76))
random.shuffle(a)
def pop(x):
while 1:
b = a.pop(0)
if b <= 15:
print 'b', b,
elif b > 15 and b <=30:
print 'i', b,
elif b > 30 and b <=45:
print 'n', b,
elif b > 45 and b <=60:
print 'g', b,
else:
print 'o', b,
raw_input('')
pop(1)
Now her parties are getting larger and she is worried about someone cheating so I would like to add in a feature where I could type in the numbers that supposedly won and have the program let me know if those numbers where indeed called. I have tried running it to a file with
def pop(x):
while 1:
b = a.pop(0)
with open('bingo.txt', 'w') as file1:
file1.write(b,'\n')
if b ...
but it just opens and then closes back down. No errors or any text to the file. It doesn’t need to go to a file. I just thought that if it did, I could then add in a searchfile feature at the end and know whether the number was called or not.
What would be the best way to find out if the numbers on the “winning” line were actually called?
I would accumulate the numbers called in a
setwhich would get returned frompop. then I’d add a function to read in the “winning” numbers (into a set) and check if that new set is a subset of the numbers which were called:This is made a little more robust by making sure that
verifysucceeds (e.g. that your user doesn’t accidentally enter a number as15a,17,22,...)As a side note, your attempt at using a file failed because you were opening a file, writing a number to it and closing it each time you “draw” a new number. Since you’re opening the file as
'w'any file that is already there gets clobbered — and so you can only ever store the last number drawn by that method. You’d want to move yourwhileloop into thewithstatement in order to get that method to work properly (or open the file for appending ('a'), but repeatedly opening a file for appending is typically not a good solution).