Yeah I understand don't worry, many of us have gone though this. What is important is to learn how to improve, learn what one should not do before doing such things in a released app. It may seem harsh at first, but really we don't hate you at all. And it's not like if it was a code on production, that might be widely used.
So it's alright. We make a bit of fun of this code and help you learn how to avoid it, and that's it 😉
u/RoboticPlayer has a really good solution in one of the other threads:
def choseTag(tag):
if tag in selectedTags:
selectedTags.remove(tag)
else:
selectedTags.append(tag)
print(selectedTags)
So where you call those different functions, instead just call this function and give it the name of the tag as a parameter.
For example using choseTag('Comedy') instead of choseTagComedy() would have the same result but it means that you don't have to worry about making new functions to do the same logic you've already made.
As a general rule of thumb, if you're running the same code with subtle differences multiple times you can probably write a more abstract (less specific) function that you can use multiple times.
It helps reduce the length of the code whilst improving flexibility of use and can also help prevent errors made when you copy and paste the same thing and make a slightly incorrect change.
101
u/Big16th Apr 24 '21
I wish I knew how to code