Answer to Question #188097 in Python for Hari nadh babu

Question #188097

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.


Input 1:-

I am 25 years and 10 months old

Output 1:-

35

17.5


Input 2:-

Tech Foundation 35567

Output 2:-

35567

35567.0


Input 3:-

Anjali25 is python4 Expert

Output 3:-

29

14.5


We want given both three inputs they can get both three outputs we code was run one by one input and output


1
Expert's answer
2021-05-02T06:07:10-0400
def SumAndAverage(str1):
    # A temporary string
    temp = "0"
    # holds sum of all numbers present in the string
    Sum = 0
    #counter of numbers in row
    count=0
    # read each char
    for ch in str1:
        # if  char is a digit
        if (ch.isdigit()):
            temp += ch
        # if current character is not digit
        else:
            if temp!="0":
                count+=1
            Sum += int(temp)
            # reset temporary string
            temp = "0"
    Sum += int(temp)
    # print sum of numbers
    print(Sum)
    if str1[-1].isdigit():
        count+=1
    # print average of nubers
    print(round(Sum / count,2))


# Main code

# input the string
print("Input the string")
str1 = input()
# Function call
SumAndAverage(str1)

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