samedi 21 août 2021

while loop, try except and other functions having issues

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. Here is a reference image 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()
    print(str(row1Num) + " is selected for row1")
    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()
    print(str(row2Num) + " is selected for row2")
    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()
    print(str(row3Num) + " is selected for row3")
    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()
    print(str(row4Num) + " is selected for row4")
    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()
    print(str(row5Num) + " is selected for row5")
    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()
    print(str(row6Num) + " is selected for row6")
    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()
    print(str(row7Num) + " is selected for row7")
    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()
    print(str(row8Num) + " is selected for row8")
    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()
    print(str(row9Num) + " is selected for row9")
    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()
    print(row1, row2, row3, row4, row5, row6, row7, row8, row9)
    
    # 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 randomNumGiver == 1 and numOfNums > 0 and row1.__len__() < 3:
            ranRow1NumGen()
        elif randomNumGiver == 2 and numOfNums > 0 and row2.__len__() < 3:
            ranRow2NumGen()
        elif randomNumGiver == 3 and numOfNums > 0 and row3.__len__() < 3:
            ranRow3NumGen()
        elif randomNumGiver == 4 and numOfNums > 0 and row4.__len__() < 3:
            ranRow4NumGen()
        elif randomNumGiver == 5 and numOfNums > 0 and row5.__len__() < 3:
            ranRow5NumGen()
        elif randomNumGiver == 6 and numOfNums > 0 and row6.__len__() < 3:
            ranRow6NumGen()
        elif randomNumGiver == 7 and numOfNums > 0 and row7.__len__() < 3:
            ranRow7NumGen()
        elif randomNumGiver == 8 and numOfNums > 0 and row8.__len__() < 3:
            ranRow8NumGen()
        elif randomNumGiver == 9 and numOfNums > 0 and row9.__len__() < 3:
            ranRow9NumGen()
        else:
            #u = u-1
            # had no idea what would work so I added the upper line
            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)
    print(allNum)
    pass

ticketGen()

I have gotten some mixed output : Incorrect output given by the code :

Script is ready
1 is selected for row1
11 is selected for row2
24 is selected for row3
31 is selected for row4
40 is selected for row5
59 is selected for row6
60 is selected for row7
70 is selected for row8
82 is selected for row9
[1] [11] [24] [31] [40] [59] [60] [70] [82]
13 is selected for row2
14 is selected for row2
13 is selected for row2
33 is selected for row4
25 is selected for row3
71 is selected for row8
70 is selected for row8
[1] [11, 13, 14, 13] [24, 25] [31, 33] [40] [59] [60] [70, 71, 70] [82]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 16, 17, 18, 19, 20, 21, 22, 23, 26, 27, 28, 29, 30, 32, 34, 35, 36, 37, 38, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 
53, 54, 55, 56, 57, 58, 61, 62, 63, 64, 65, 66, 67, 68, 69, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90]

Correct way and the output given by the code :

Script is ready
5 is selected for row1
15 is selected for row2
28 is selected for row3
35 is selected for row4
40 is selected for row5
51 is selected for row6
68 is selected for row7
77 is selected for row8
86 is selected for row9
[5] [15] [28] [35] [40] [51] [68] [77] [86]
90 is selected for row9
72 is selected for row8
6 is selected for row1
87 is selected for row9
20 is selected for row3
[5, 6] [15] [28, 20] [35, 34] [40] [51] [68] [77, 72] [86, 90, 87]
[1, 2, 3, 4, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31, 32, 33, 36, 37, 38, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 69, 70, 71, 73, 74, 75, 76, 78, 79, 80, 81, 82, 83, 84, 85, 88, 89]

As you can see it has a mixed output. The issue with the first output is the numbers cant 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

Aucun commentaire:

Enregistrer un commentaire