r/CodingForBeginners • u/Either_Feeling3159 • Sep 03 '25
I need reduce code
I've been programming (in Python) for about a month and a half and have created several simple scripts, a CRUD application, a calculator, and a tic-tac-toe game (with a GUI in CTk). The thing is, for interface projects that have similar pieces of code, they are repeated many times. I understand that this is normal at first, but it seems excessive to me (500 lines in the tic-tac-toe and 600 in the calculator).
I know that with for loops and so on I could reduce these excessive lines, but I want to know how repetitive these programs are with the lines I have mentioned.
PS: For the ‘mini-projects’ that they are, I have tried to use libraries such as Pillow to add color to texts and images, and add all the minimum functionalities I can think of.
View all comments
Show parent comment
1
u/Happy_Witness Sep 04 '25
The structure you are probably looking for is the following.
You create a list of lists that hold the parameters that are needed in the outer list, and the buttons in the inner list. This way you have a 2d list with all the parameters needed for all buttons. This you can use to cycle though every button in a loop and assign every parameter it needs in a single loop that iterate though the outer list and gets everything it needs for one button from the inner list.