[Swlug] Quick Python question

Ian Hill ian at cellar.org.uk
Thu Feb 3 07:56:03 UTC 2022


On Thu, 3 Feb 2022 at 01:58, Rhys Sage via Swlug
<swlug at mailman.lug.org.uk> wrote:
> if ((NumberOne.isnumeric == False) and (NumberTwo.isnumeric == False)):
>     print("Retard - I asked for numbers")
>     print (NumberOne.isnumeric())
>     print (NumberTwo.isnumeric())

Couple of things...

* isnumeric() is a function not a property so you have to call it
* == comparison to True/False does work but isn't pythonic, you can
just use logical operators
* as Colin has said, you are only checking if BOTH numbers are not
numeric, I suppose you need either or.

This should work:

NumberOne = input("Number 1: ")
NumberTwo = input("Number 2: ")
if not (NumberOne.isnumeric() and NumberTwo.isnumeric()):
    print("Those weren't numbers")

Thanks,
Ian



More information about the Swlug mailing list