0

I have this string and I want to calculate the sum, but it doesn't show the right answer, it prints 51.

sumAndAverage = "English = 78 Science = 83 Math = 68 History = 65"

def sum_digits_string(str1):
    sum_digit = 0
    for x in str1:
        if x.isdigit():
            z = int(x)
            sum_digit = sum_digit + z

    return sum_digit 

print(sum_digits_string(sumAndAverage))
4
  • 1
    it calculates the sum of all the digits (7+8+8+3+6+8+6+5). do you want to calculate the sum of all numbers? Commented Apr 17, 2020 at 7:15
  • well, 7+8+8+3+6+8+6+5 = 51 :-) sum of digits. what you expect? Commented Apr 17, 2020 at 7:15
  • Your program sums the individual digits not "whole" number Commented Apr 17, 2020 at 7:15
  • that's because it reads each digit and adds it, you have 7+8+8+3+6+8+6+5 Commented Apr 17, 2020 at 7:18

7 Answers 7

1

The best (Pythonic) way to do it using list comprehension and sum(). Try this:

sumAndAverage = "English = 78 Science = 83 Math = 68 History = 65"

li = [int(x) for x in sumAndAverage if x.isdigit()]
print("List:", li, "=", sum(li))

Output:

List: [7, 8, 8, 3, 6, 8, 6, 5] = 51

If you want to calculate the sum of numbers in your string:

sumAndAverage = "English = 78 Science = 83 Math = 68 History = 65"

li = [int(x) for x in sumAndAverage.split() if x.isdigit()]
print("List:", li, "=", sum(li))

Output:

List: [78, 83, 68, 65] = 294
Sign up to request clarification or add additional context in comments.

1 Comment

OP code does the same. Obviously their problem statement is not correct
1

Try using re:

sumAndAverage = "English = 78 Science = 83 Math = 68 History = 65"
nums = re.findall('\d+', sumAndAverage)
nums = [int(num) for num in nums]
print(sum(nums))
# 294

Regex will try and find all numbers in your string, store them to a list, then you just need to sum them up.

Comments

0

Here you have a possible solution. Now you have as a result 229 (78 + 83 + 65).

The problem with your code was that you were summing all the numbers in the string, but you did not consider that number may be more than one character in length.

sumAndAverage = "English = 78 Science = 83 Math = 68 History = 65"

def sum_digits_string(str1):
    sum_digit = 0
    num = ""
    for x in str1:
        if x.isdigit():
            num += x
        else:
            if num != "":
                z = int(num)
                sum_digit += z
                num = ""
    return sum_digit 

print(sum_digits_string(sumAndAverage))

Comments

0

It's because you're casting each digit separately, so what you're doing is 7 + 8 + 8 + 3 + 6 + 8 + 6 + 5 which is indeed 51. What you want to do here is splitting your string and casting the entire numbers, try something like this :

sumAndAverage = "English = 78 Science = 83 Math = 68 History = 65"

def sum_digits_string(str1):
    sum_digit = 0

    for x in str1.split(" "):
        if x.isdigit():
            z = int(x)
            sum_digit = sum_digit + z

    return sum_digit 

print(sum_digits_string(sumAndAverage))

That is working an giving 294.

Comments

0
str = 'English = 78 Science = 83 Math = 68 History = 65'    
a = sum([int(s) for s in str.split() if s.isdigit()])
print(a)

Comments

0

Your current code is printing sum of individual digits and not numbers. In your case, you can use split() method to achieve what you intend to do:

sumAndAverage = "English = 78 Science = 83 Math = 68 History = 65"

def sum_digits_string(str1):
    sum_digit = 0

    for x in str1.split():
        if x.isdigit():
            sum_digit += int(x)

    return sum_digit

print(sum_digits_string(sumAndAverage))

Output: 294

Comments

0

You can also achieve this with a single line of code

sumAndAverage = "English = 78 Science = 83 Math = 68 History = 65"

Solution 1:

from functools import reduce

>>> reduce(lambda a, x: int(a)+int(x), [int(s) for s in sumAndAverage.split() if s.isdigit()])
294

Solution 2:

>>> sum([int(s) for s in sumAndAverage.split() if s.isdigit()])
294

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.