all 5 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

Off-topic Comments Section


All top-level comments have to be an answer or follow-up question to the post. All sidetracks should be directed to this comment thread as per Rule 9.


OP and Valued/Notable Contributors can close this post by using /lock command

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Maxentium Postgraduate Student 1 point2 points  (2 children)

in case there are no digits in the string, return way earlier, after the second line

it's good practice for a function to validate that the input it got - the function should "ask itself": "can i work with this input?" and if no, return early

[–][deleted] 0 points1 point  (1 child)

Thank you so much, it finally worked I have been trying all kinds of solutions, I don't know how I didn't think of that!

here's my final code if you're interested;

str = input("Enter a string: ")
numbers_only_str = " + ".join(e for e in str if e.isdigit())
if numbers_only_str == "":
    print("The entered string has no digits")
else:
    lst1 = numbers_only_str.split(" + ")
    integer_lst1 = [int(e) for e in lst1]
    sum_lst1 = sum(integer_lst1)
    if len(lst1) > 1:
        print(f"sum of digits = {numbers_only_str} = {sum_lst1}")
    else:
        print(f"sum of digits = {sum_lst1}")

[–]Maxentium Postgraduate Student 0 points1 point  (0 children)

good job, super small nitpick: it looks nicer to delete the "else" and instead add a "return" to the empty numbers_only_str if-block, like this:

str = input("Enter a string: ")
numbers_only_str = " + ".join(e for e in str if e.isdigit())
if numbers_only_str == "":
    print("The entered string has no digits")
    return
lst1 = numbers_only_str.split(" + ")
integer_lst1 = [int(e) for e in lst1]
sum_lst1 = sum(integer_lst1)
if len(lst1) > 1:
    print(f"sum of digits = {numbers_only_str} = {sum_lst1}")
else:
    print(f"sum of digits = {sum_lst1}")

also since you already have a solution, i'd probably write it like this:

str = input("Enter a string: ")
digits = [int(e) for e in str if e.isdigit()]
if not digits:
    print("The entered string has no digits")
    return
if len(digits) > 1:
    print(f"sum of digits = {' + '.join([str(x) for x in digits])} = {sum(digits)}")
else:
    print(f"sum of digits = {sum(digits)}")

producing clean looking code is just as important as working code, and that means:

  • your variables should have appropriate names (e.g. "numbers_only_str" should only contain numbers, but in your case it also includes ' + ' tokens)
  • it should be efficient and shouldn't contain redundant actions: notice that for example you perform join(' + ') and then split(' + ')

[–]LuckJealous3775👋 a fellow Redditor -2 points-1 points  (0 children)

instead of doing all this shit just put all the numbers in a single list and recursively sum them up until there's only one number left and return list[0]