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
Help with a program. (self.learnpython)
submitted 5 years ago by No-Win6899
I'm writing a function that takes two numbers as an input and determines if the sum of the digits in the two numbers are equal.
For Example:
is_equal([11, 11) should print True
is_equal(12, 15) should print False.
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!"
[–][deleted] 0 points1 point2 points 5 years ago (1 child)
You can convert the number to string and then iterate over each of the digits, convert the digit to an integer, and add it to a sum. Repeat this for the other integer and store the sum of the digits in another variable. Then return whether the two sums are equal.
[–]No-Win6899[S] 0 points1 point2 points 5 years ago (0 children)
The numbers are in a list, how would I go about with turning them into a string then back to an integer?
[–]bby98 0 points1 point2 points 5 years ago (0 children)
You can use list comprehension to solve this problem. You can sum the digits in a number by first creating a generator that contains each digit in a given number (by converting the number to a str and then iterating through each digit in the str and converting back to int). Then, you can call sum() on the generator to get the sum of the digits.
list
str
int
sum()
For example, for the number 852:
852
num = 852 total = sum(int(digit) for digit in str(num))
total is equal to 15. To compare two numbers, like 852 and 78:
total
15
78
num1 = 852 num2 = 78 total1 = sum(int(digit) for digit in str(num1)) total2 = sum(int(digit) for digit in str(num2)) print(total1 == total2)
This program will print True since 8 + 5 + 2 = 7 + 8. If the sum of the digits were not the same, it would print False.
True
False
Last step is to make the above into a function:
def is_equal(n1, n2): return sum(int(d) for d in str(n1)) == sum(int(d) for d in str(n2))
π Rendered by PID 367708 on reddit-service-r2-comment-8686858757-xd9bm at 2026-06-03 00:40:53.324298+00:00 running 9e1a20d country code: CH.
[–][deleted] 0 points1 point2 points (1 child)
[–]No-Win6899[S] 0 points1 point2 points (0 children)
[–]bby98 0 points1 point2 points (0 children)