Is there a way to condense this code? /u/Dragonmanenderr Python Education

 if len(self.hiddenWord) >= 1: self.letter1 = ttk.Label(self.Hangman_Game, text='_', font=('OCR A Extended', 20, 'bold')) self.letter1.grid(row=1, column=int(self.centre - (len(self.hiddenWord))/2), sticky='nsew') if len(self.hiddenWord) >= 2: self.letter2 = ttk.Label(self.Hangman_Game, text='_', font=('OCR A Extended', 20, 'bold')) self.letter2.grid(row=1, column=int(self.centre - (len(self.hiddenWord))/2)+1, sticky='nsew') if len(self.hiddenWord) >= 3: self.letter3 = ttk.Label(self.Hangman_Game, text='_', font=('OCR A Extended', 20, 'bold')) self.letter3.grid(row=1, column=int(self.centre - (len(self.hiddenWord))/2)+2, sticky='nsew') if len(self.hiddenWord) >= 4: self.letter4 = ttk.Label(self.Hangman_Game, text='_', font=('OCR A Extended', 20, 'bold')) self.letter4.grid(row=1, column=int(self.centre - (len(self.hiddenWord))/2)+3, sticky='nsew') 

(this is just a fraction of the repeating code, it goes on like this for 14 repetitions)

Is there any way to condense code like this? this example of mine is from a hangman game i was creating for a school project, and the overall outcome was pretty good. However, the biggest problem with my code is that I wasn’t able to condense code where multiple variables were being changed in the same way.

My first thought was to use for loops, like so:

for i in range(n): if len(self.hiddenWord) >= i+1: self.letter1 = ttk.Label(self.Hangman_Game, text='_', font=('OCR A Extended', 20, 'bold')) self.letter1.grid(row=1, column=int(self.centre - (len(self.hiddenWord))/2)+i, sticky='nsew') 

but then that gives me no way to change which label i am modifying. (letter1, letter2, etc.)

This problem has popped up quite a few times in my code, and i am unsure of how to solve it. Any help on this or pointers towards tutorials would be greatly appreciated!

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

​r/learnpython if len(self.hiddenWord) >= 1: self.letter1 = ttk.Label(self.Hangman_Game, text=’_’, font=(‘OCR A Extended’, 20, ‘bold’)) self.letter1.grid(row=1, column=int(self.centre – (len(self.hiddenWord))/2), sticky=’nsew’) if len(self.hiddenWord) >= 2: self.letter2 = ttk.Label(self.Hangman_Game, text=’_’, font=(‘OCR A Extended’, 20, ‘bold’)) self.letter2.grid(row=1, column=int(self.centre – (len(self.hiddenWord))/2)+1, sticky=’nsew’) if len(self.hiddenWord) >= 3: self.letter3 = ttk.Label(self.Hangman_Game, text=’_’, font=(‘OCR A Extended’, 20, ‘bold’)) self.letter3.grid(row=1, column=int(self.centre – (len(self.hiddenWord))/2)+2, sticky=’nsew’) if len(self.hiddenWord) >= 4: self.letter4 = ttk.Label(self.Hangman_Game, text=’_’, font=(‘OCR A Extended’, 20, ‘bold’)) self.letter4.grid(row=1, column=int(self.centre – (len(self.hiddenWord))/2)+3, sticky=’nsew’) (this is just a fraction of the repeating code, it goes on like this for 14 repetitions) Is there any way to condense code like this? this example of mine is from a hangman game i was creating for a school project, and the overall outcome was pretty good. However, the biggest problem with my code is that I wasn’t able to condense code where multiple variables were being changed in the same way. My first thought was to use for loops, like so: for i in range(n): if len(self.hiddenWord) >= i+1: self.letter1 = ttk.Label(self.Hangman_Game, text=’_’, font=(‘OCR A Extended’, 20, ‘bold’)) self.letter1.grid(row=1, column=int(self.centre – (len(self.hiddenWord))/2)+i, sticky=’nsew’) but then that gives me no way to change which label i am modifying. (letter1, letter2, etc.) This problem has popped up quite a few times in my code, and i am unsure of how to solve it. Any help on this or pointers towards tutorials would be greatly appreciated! submitted by /u/Dragonmanenderr [link] [comments] 

 if len(self.hiddenWord) >= 1: self.letter1 = ttk.Label(self.Hangman_Game, text='_', font=('OCR A Extended', 20, 'bold')) self.letter1.grid(row=1, column=int(self.centre - (len(self.hiddenWord))/2), sticky='nsew') if len(self.hiddenWord) >= 2: self.letter2 = ttk.Label(self.Hangman_Game, text='_', font=('OCR A Extended', 20, 'bold')) self.letter2.grid(row=1, column=int(self.centre - (len(self.hiddenWord))/2)+1, sticky='nsew') if len(self.hiddenWord) >= 3: self.letter3 = ttk.Label(self.Hangman_Game, text='_', font=('OCR A Extended', 20, 'bold')) self.letter3.grid(row=1, column=int(self.centre - (len(self.hiddenWord))/2)+2, sticky='nsew') if len(self.hiddenWord) >= 4: self.letter4 = ttk.Label(self.Hangman_Game, text='_', font=('OCR A Extended', 20, 'bold')) self.letter4.grid(row=1, column=int(self.centre - (len(self.hiddenWord))/2)+3, sticky='nsew') 

(this is just a fraction of the repeating code, it goes on like this for 14 repetitions)

Is there any way to condense code like this? this example of mine is from a hangman game i was creating for a school project, and the overall outcome was pretty good. However, the biggest problem with my code is that I wasn’t able to condense code where multiple variables were being changed in the same way.

My first thought was to use for loops, like so:

for i in range(n): if len(self.hiddenWord) >= i+1: self.letter1 = ttk.Label(self.Hangman_Game, text='_', font=('OCR A Extended', 20, 'bold')) self.letter1.grid(row=1, column=int(self.centre - (len(self.hiddenWord))/2)+i, sticky='nsew') 

but then that gives me no way to change which label i am modifying. (letter1, letter2, etc.)

This problem has popped up quite a few times in my code, and i am unsure of how to solve it. Any help on this or pointers towards tutorials would be greatly appreciated!

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

Leave a Reply

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