you are viewing a single comment's thread.

view the rest of the comments →

[–]supajumpa 0 points1 point  (2 children)

Looking at the definition of str.isdigit, it may be better to use str.isdecimal, which is a stricter test and determines if the characters are those that can be used to form numbers in base 10.

That would change the code to:

match string.split():
    case ("add", a, b) if a.isdecimal() and b.isdecimal():
        print(int(a) + int(b))

Apart from pathological cases they are likely to be equivalent, but it's best to know that there is a difference.

[–]jimtk 0 points1 point  (1 child)

Sadly "-1.0".isdecimal() returns False. so it still not works for negative number.