Answer to Question #188507 in Python for mani

Question #188507

Numbers in String - 2

Given a string, write a program to return the sum and average of the numbers that appear in the string, ignoring all other characters.


Input


The input will be a single line containing a string.


Output


The output should contain the sum and average of the numbers that appear in the string.

Note: Round the average value to two decimal places.


Explanation


For example, if the given string is "I am 25 years and 10 months old", the numbers are 25, 10. Your code should print the sum of the numbers(35) and the average of the numbers(17.5) in the new line.


Sample Input 1

I am 25 years and 10 months old


Sample Output 1

35

17.5


Sample Input 2

Tech Foundation 35567


Sample Output 2

35567

35567.0




1
Expert's answer
2021-05-03T07:53:58-0400
def SumAndAverage(myString):
    temp = "0"
    sum1 = 0
    count=0

    for character in myString:

        if (character.isdigit()):
            temp += character
        else:
            if temp!="0":
                count+=1
            sum1 += int(temp)
            temp = "0"

    sum1 += int(temp)
    print("Sum:", sum1)
    average=sum1/count
    print("Average: {:.2f}".format(average))

if __name__ == '__main__':
    string1 = input("Enter a string:")
    SumAndAverage(string1)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS