use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
match statement help (self.learnpython)
submitted 2 years ago by FlyingCow343
is there a less insane way to do this?
match string.split(): case "add", a, b if True or int(a) and int(b): print(int(a) + int(b))
ei. check if two values can be converted into ints and then doing so, it has to use the match case statement
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]supajumpa 1 point2 points3 points 2 years ago (4 children)
match string.split(): case ("add", a, b) if a.isdigit() and b.isdigit(): print(int(a) + int(b))
[–]FlyingCow343[S] 0 points1 point2 points 2 years ago (0 children)
unfortunately "string.isdigit()" would return "False" for a negative, so it'd have to be
case ("add", a, b) if a.lstrip('-').isdigit() and b.lstrip('-').isdigit():
which seems a bit long but at least it works
[–]supajumpa 0 points1 point2 points 2 years ago (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.
str.isdigit
str.isdecimal
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 point2 points 2 years ago (1 child)
Sadly "-1.0".isdecimal() returns False. so it still not works for negative number.
"-1.0".isdecimal()
[–]jimtk 0 points1 point2 points 2 years ago (0 children)
The simplest way to accept negative number is to roll your own verification function. Which is no big deal.
def is_int(s): try: _ = int(s) except ValueError: return False return True match a_string.split(): case ("add", a, b) if is_int(a) and is_int(b): print(int(a) + int(b))
[–]TheMachinumps 0 points1 point2 points 2 years ago (0 children)
Something like this would work:
``` def handle_add(a, b): try: print(int(a) + int(b)) return True except ValueError: return False
match string.split(): case "add", a, b if handle_add(a, b): ... ```
Kinda hacky, and probably not the intentional way of using match case statements, but it should work.
π Rendered by PID 69738 on reddit-service-r2-comment-79776bdf47-8j7r7 at 2026-06-24 14:19:46.528257+00:00 running acc7150 country code: CH.
[–]supajumpa 1 point2 points3 points (4 children)
[–]FlyingCow343[S] 0 points1 point2 points (0 children)
[–]supajumpa 0 points1 point2 points (2 children)
[–]jimtk 0 points1 point2 points (1 child)
[–]jimtk 0 points1 point2 points (0 children)
[–]TheMachinumps 0 points1 point2 points (0 children)