I am creating a bingo/housie ticket creator in python. The tickets have 9 rows and 3 columns. It means 27 spaces on a 9*3 grid. 9 rows vertically and 3 columns horizontally. Each row can only hold 3 random numbers. The first row can hold a random number from 1,9. The second row can hold a random number from 10,19. The third row can hold a random number from 20,29. The fourth row can hold a random number from 30,39. The fifth row can hold a random number from 40,49. The sixth row can hold a random number from 50,59. The seventh row can hold a random number from 60,69. The eighth row can hold a random number from 70,79. The ninth row can hold a random number from 80,90. Here is a reference image of the ticket.
I am planning to create a random ticket in python. Here is the script I created for it. It has all the comments for better understanding.
import random
# print("Script is ready")
# Variables
global numOfNums # This tells that only 15 numbers can be added to the ticket
numOfNums = 15
global allNum # this is the list containing all 90 numbers
global numToBedone # this is just for adding nums to the list
allNum = []
numToBedone = 1
global row1, row2, row3, row4, row5, row6, row7, row8, row9 # all the rows
row1, row2, row3, row4, row5, row6, row7, row8, row9 = [], [], [], [], [], [], [], [], []
global row1Num, row2Num, row3Num, row4Num, row5Num, row6Num, row7Num, row8Num, row9Num # all the nums row can have
# Filling the List
while numToBedone<91:
allNum.append(numToBedone)
numToBedone = numToBedone+1
# Generating number for row 1
def ranRow1NumGen():
global numOfNums
row1Num = random.randint(1,9)
try:
allNum.remove(row1Num)
except:
ranRow1NumGen()
row1.append(row1Num)
numOfNums = numOfNums - 1
# Generating number for row 2
def ranRow2NumGen():
global numOfNums
row2Num = random.randint(10,19)
try:
allNum.remove(row2Num)
except:
ranRow2NumGen()
row2.append(row2Num)
numOfNums = numOfNums - 1
# Generating number for row 3
def ranRow3NumGen():
global numOfNums
row3Num = random.randint(20,29)
try:
allNum.remove(row3Num)
except:
ranRow3NumGen()
row3.append(row3Num)
numOfNums = numOfNums - 1
# Generating number for row 4
def ranRow4NumGen():
global numOfNums
row4Num = random.randint(30,39)
try:
allNum.remove(row4Num)
except:
ranRow4NumGen()
row4.append(row4Num)
numOfNums = numOfNums - 1
# Generating number for row 5
def ranRow5NumGen():
global numOfNums
row5Num = random.randint(40,49)
try:
allNum.remove(row5Num)
except:
ranRow5NumGen()
row5.append(row5Num)
numOfNums = numOfNums - 1
# Generating number for row 6
def ranRow6NumGen():
global numOfNums
row6Num = random.randint(50,59)
try:
allNum.remove(row6Num)
except:
ranRow6NumGen()
row6.append(row6Num)
numOfNums = numOfNums - 1
# Generating number for row 7
def ranRow7NumGen():
global numOfNums
row7Num = random.randint(60,69)
try:
allNum.remove(row7Num)
except:
ranRow7NumGen()
row7.append(row7Num)
numOfNums = numOfNums - 1
# Generating number for row 8
def ranRow8NumGen():
global numOfNums
row8Num = random.randint(70,79)
try:
allNum.remove(row8Num)
except:
ranRow8NumGen()
row8.append(row8Num)
numOfNums = numOfNums - 1
# Generating number for row 9
def ranRow9NumGen():
global numOfNums
row9Num = random.randint(80,90)
try:
allNum.remove(row9Num)
except:
ranRow9NumGen()
row9.append(row9Num)
numOfNums = numOfNums - 1
# Main function for creating a ticket
def ticketGen():
global row1, row2, row3, row4, row5, row6, row7, row8, row9
global numOfNums
#Adding 1 number to all the rows
ranRow1NumGen()
ranRow2NumGen()
ranRow3NumGen()
ranRow4NumGen()
ranRow5NumGen()
ranRow6NumGen()
ranRow7NumGen()
ranRow8NumGen()
ranRow9NumGen()
# After we put 1 number in each row we have to put the rest in the random rows
# I cant understand how to do that
# I used this way but it has some issues
# sometimes a row can get more than 3 num which isnt possible in the ticket
# also some numbers repeat but I cant understand how as I have a try except statement
u = 0
while u < 6:
randomNumGiver = random.randint(1,9)
if numOfNums > 0:
if randomNumGiver == 1 and len(row1) < 3:
ranRow1NumGen()
elif randomNumGiver == 2 and len(row2) < 3:
ranRow2NumGen()
elif randomNumGiver == 3 and len(row3) < 3:
ranRow3NumGen()
elif randomNumGiver == 4 and len(row4) < 3:
ranRow4NumGen()
elif randomNumGiver == 5 and len(row5) < 3:
ranRow5NumGen()
elif randomNumGiver == 6 and len(row6) < 3:
ranRow6NumGen()
elif randomNumGiver == 7 and len(row7) < 3:
ranRow7NumGen()
elif randomNumGiver == 8 and len(row8) < 3:
ranRow8NumGen()
elif randomNumGiver == 9 and len(row9) < 3:
ranRow9NumGen()
else:
pass
u = u+1
# printing all the rows and the remaining numbers in the list
print(row1, row2, row3, row4, row5, row6, row7, row8, row9)
pass
ticketGen()
I have gotten some mixed output : Incorrect output given by the code :
[8] [14] [21, 25, 21] [32] [42, 47, 42] [55] [60, 66, 60] [76] [82]
Correct way and the output given by the code :
[7, 2] [16, 14] [28] [34] [40] [59, 56, 52] [68, 61, 64] [77] [83]
As you can see it has a mixed output. The issue with the first output is the numbers can't be repeated and also a row can't have more than 3 numbers. I can't find how this bug is happening as I have set a try-except statement for the repetition and an if statement for having less than 3 numbers. If you can find any issue and know the way to resolve it please do so. It would mean a lot to me. Thank you.
Edit - The code has now been changed according to the first answer but I got these results. Also at one point it clearly had more than 3 numbers in a row and they were duplicate numbers. Can't figure out a way please HELP