0

This is the code that i was trying to execute and it works good when i click on button for 55 times but on the 56 attempt it crashes. I am not able to understand why is it so. please help why it is crashing and how to solve this. I was trying to make a tambola board using tkinter. and should i use tkinter

from tkinter import *
import random

main_var = []
done_var = []

for a in range(1, 91):
    main_var.append(a)


def board():
    increas = 1
    for x in range(10, 495, 55):
        for y in range(10, 700, 70):
            frame = Frame(
                master=window,
                relief=RAISED,
                borderwidth=2,
                bg="orange"

            )
            frame.place(x=y, y=x)
            num_label = Label(master=frame, text=increas, borderwidth=2, bg="orange", fg="white", height=3,
                              width=9)
            num_label.pack()
            increas += 1


def num_generate():

    random_num = random.choice(main_var)
    num.set(random_num)
    print(random_num)
    main_var.remove(random_num)
    done_var.append(random_num)
    increas = 1
    for x in range(10, 495, 55):
        for y in range(10, 700, 70):
            if increas in done_var:
                frame = Frame(
                    master=window,
                    relief=RIDGE,
                    borderwidth=2,
                    bg="green"

                )
                frame.place(x=y, y=x)
                num_label = Label(master=frame, text=increas, borderwidth=2, bg="green", fg="white", height=3,
                                  width=9)
                num_label.pack()
                increas += 1

            else:
                frame = Frame(
                    master=window,
                    relief=RAISED,
                    borderwidth=2,
                    bg="orange"

                )
                frame.place(x=y, y=x)
                num_label = Label(master=frame, text=increas, borderwidth=2, bg="orange", fg="white", height=3,
                                  width=9)
                num_label.pack()
                increas += 1


# initialising
window = Tk()
window.geometry("1000x1000")
window.title("Tambola Game")


# for adding values from 1 to 90


# board function
board()


# Number Generator and marking on board
num = StringVar()
num_generate = Button(text="Generate", height=3, width=70, bg="red", fg="white", command=num_generate)
num_generate.place(x=770, y=500)
Label(textvariable=num, fg="dark blue", font="Algerian 200 bold").place(x=855, y=100)

# Orange = Not done, Green = Done
user_tell = Frame(relief=GROOVE, borderwidth=2)
user_tell.place(x=10, y=550)
label = Label(user_tell, text="PENDING NUMBERS", bg="Orange", width=25, fg="black", font="Verdana")
label.pack()
label = Label(user_tell, text="CALLED NUMBERS", bg="green", width=25, fg="black", font="Verdana")
label.pack()

# offers menu


window.mainloop()

1 Answer 1

1

It is not crashing without an error, it is raising the following exception : Cannot choose from an empty sequence.

This means that the main_var is empty (you have removed every element from it). You should check for it before trying to remove an element :

def num_generate():

    if len(main_var) > 0:
        random_num = random.choice(main_var)
        num.set(random_num)
        print(random_num)
        main_var.remove(random_num)

    else:
        # do something else

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.