Calling Functions with help of Dictionary /u/manudon01 Python Education

So I was creating a basic tic-tac-toe game and while I was designing the UI I created 9 separate buttons with the help of tkinter for inputting the position of X and O. With the help of for loop I was able to create 9 separate buttons and store them into a list. I then want to write commands to print X and O on the window with the help of the same for loop so I don’t need to create separate loops for calling different functions for setting X and O.

from tkinter import * # ---------------------------- CONSTANTS ------------------------------- # PINK = "#e2979c" RED = "#e7305b" GREEN = "#9bdeac" YELLOW = "#f7f5dd" FONT_NAME = "American Typewriter" count = 0 def assign(index): if count%2 == 0: buttons[index].config(text="X") else: buttons[index].config(text="O") # ---------------------------- GAME CODE ------------------------------- # window = Tk() window.title("Mini Games") window.minsize(width=1000, height=600) gameTitle= Label(text="TicTacToe", fg=RED, font=(FONT_NAME, 50, "bold")) gameTitle.place(x=370, y=50) buttons = [] assigning = { 0 : assign(0), 1 : assign(1), 2 : assign(2), 3 : assign(3), 4 : assign(4), 5 : assign(5), 6 : assign(6), 7 : assign(7), 8 : assign(8) } print(buttons) for i in range(0, 3): for j in range(0,3): temp = Button(highlightthickness=0, command=assigning[i*3 + j]) buttons.append(temp) print(buttons) buttons[i*3 + j].place(x=250*j + 210, y=100*i + 200) buttons[i*3 + j].config(padx=25, pady=25) window.mainloop() 

I think I am doing something wrong but I can’t figure out exactly what is it. I really don’t know much about syntax of calling the function but it doesn’t give me any syntax error. This is the exact error which I got,

Traceback (most recent call last):

File “/Users/manan/Desktop/python projects/personal files/main.py”, line 30, in <module>

0 : assign(0),

^^^^^^^^^

File “/Users/manan/Desktop/python projects/personal files/main.py”, line 14, in assign

buttons[index].config(text=”X”)

~~~~~~~^^^^^^^

IndexError: list index out of range

Any help or suggestions would be greatly appreciated, I want to improve my programming skills.

submitted by /u/manudon01
[link] [comments]

​r/learnpython So I was creating a basic tic-tac-toe game and while I was designing the UI I created 9 separate buttons with the help of tkinter for inputting the position of X and O. With the help of for loop I was able to create 9 separate buttons and store them into a list. I then want to write commands to print X and O on the window with the help of the same for loop so I don’t need to create separate loops for calling different functions for setting X and O. from tkinter import * # —————————- CONSTANTS ——————————- # PINK = “#e2979c” RED = “#e7305b” GREEN = “#9bdeac” YELLOW = “#f7f5dd” FONT_NAME = “American Typewriter” count = 0 def assign(index): if count%2 == 0: buttons[index].config(text=”X”) else: buttons[index].config(text=”O”) # —————————- GAME CODE ——————————- # window = Tk() window.title(“Mini Games”) window.minsize(width=1000, height=600) gameTitle= Label(text=”TicTacToe”, fg=RED, font=(FONT_NAME, 50, “bold”)) gameTitle.place(x=370, y=50) buttons = [] assigning = { 0 : assign(0), 1 : assign(1), 2 : assign(2), 3 : assign(3), 4 : assign(4), 5 : assign(5), 6 : assign(6), 7 : assign(7), 8 : assign(8) } print(buttons) for i in range(0, 3): for j in range(0,3): temp = Button(highlightthickness=0, command=assigning[i*3 + j]) buttons.append(temp) print(buttons) buttons[i*3 + j].place(x=250*j + 210, y=100*i + 200) buttons[i*3 + j].config(padx=25, pady=25) window.mainloop() I think I am doing something wrong but I can’t figure out exactly what is it. I really don’t know much about syntax of calling the function but it doesn’t give me any syntax error. This is the exact error which I got, Traceback (most recent call last): File “/Users/manan/Desktop/python projects/personal files/main.py”, line 30, in <module> 0 : assign(0), ^^^^^^^^^ File “/Users/manan/Desktop/python projects/personal files/main.py”, line 14, in assign buttons[index].config(text=”X”) ~~~~~~~^^^^^^^ IndexError: list index out of range Any help or suggestions would be greatly appreciated, I want to improve my programming skills. submitted by /u/manudon01 [link] [comments] 

So I was creating a basic tic-tac-toe game and while I was designing the UI I created 9 separate buttons with the help of tkinter for inputting the position of X and O. With the help of for loop I was able to create 9 separate buttons and store them into a list. I then want to write commands to print X and O on the window with the help of the same for loop so I don’t need to create separate loops for calling different functions for setting X and O.

from tkinter import * # ---------------------------- CONSTANTS ------------------------------- # PINK = "#e2979c" RED = "#e7305b" GREEN = "#9bdeac" YELLOW = "#f7f5dd" FONT_NAME = "American Typewriter" count = 0 def assign(index): if count%2 == 0: buttons[index].config(text="X") else: buttons[index].config(text="O") # ---------------------------- GAME CODE ------------------------------- # window = Tk() window.title("Mini Games") window.minsize(width=1000, height=600) gameTitle= Label(text="TicTacToe", fg=RED, font=(FONT_NAME, 50, "bold")) gameTitle.place(x=370, y=50) buttons = [] assigning = { 0 : assign(0), 1 : assign(1), 2 : assign(2), 3 : assign(3), 4 : assign(4), 5 : assign(5), 6 : assign(6), 7 : assign(7), 8 : assign(8) } print(buttons) for i in range(0, 3): for j in range(0,3): temp = Button(highlightthickness=0, command=assigning[i*3 + j]) buttons.append(temp) print(buttons) buttons[i*3 + j].place(x=250*j + 210, y=100*i + 200) buttons[i*3 + j].config(padx=25, pady=25) window.mainloop() 

I think I am doing something wrong but I can’t figure out exactly what is it. I really don’t know much about syntax of calling the function but it doesn’t give me any syntax error. This is the exact error which I got,

Traceback (most recent call last):

File “/Users/manan/Desktop/python projects/personal files/main.py”, line 30, in <module>

0 : assign(0),

^^^^^^^^^

File “/Users/manan/Desktop/python projects/personal files/main.py”, line 14, in assign

buttons[index].config(text=”X”)

~~~~~~~^^^^^^^

IndexError: list index out of range

Any help or suggestions would be greatly appreciated, I want to improve my programming skills.

submitted by /u/manudon01
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *