I have a simple program
temp='abc12a'
ver = 1
print(temp[-1].isdigit())
if temp[-1].isdigit() & int(temp[-1])!=ver:
print(temp[:-1]+t)
which gives the output
False
Traceback (most recent call last):
File "python", line 3, in <module>
ValueError: invalid literal for int() with base 10: 'a'
Why does the second condition even get checked when the first is false? Is there a way to only do the second if the first is true that is cleaner than two ifs?
The & operator in Python checks as a bitwise and. It does not short circuit. The and operator properly short circuits, which is the behavior you are looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With