May-06-2021, 02:04 PM
In circumstances like this where the value may be
So, instead of:
Also, since the
None
(or an empty string, list, etc.), one option is to use and
to make sure there is a value before trying isdigit()
.So, instead of:
if presets.get('last').isdigit()Do this:
if presets.get('last') and presets.get('last').isdigit()This works because Python first evaluates
if presets.get('last')
. If there is no value, the condition is False (because False and x
is False no matter what x is), so Python doesn't even evaluate beyond the "and".Also, since the
isdigit()
method returns True or False, there is no need for the comparison operator:if x.isdigit(): # accomplishes the same thing as if x.isdigit() == True: