For the example below:
if a == 100:
# Five lines of code
elif a == 200:
# Five lines of code
Five lines of code is common and repeating how can I avoid it?
I know about putting it a function
or
if a == 100 or a == 200:
# Five lines of code
if a == 100:
# Do something
elif a == 200:
# Do something
Any other cleaner solution?
Remember that with functions, you can have local functions with a closure. This means you can avoid passing repetitive arguments and still modify locals. (Just be careful with assignments in that local function. Also see the
nonlocalkeyword in Python 3.)