Answer to Question #201258 in Python for sajid pasha

Question #201258

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

sample input:

I am 25 years and 10 months old

output

37

17.5


1
Expert's answer
2021-05-31T09:16:04-0400
def sum_and_average(s1):
    sumd = 0
    count=0
    temp = "0"
    for c in s1:
        if (c.isdigit()):
            temp += c
        else:
            if temp!="0":
                count+=1
            sumd+= int(temp)
            temp = "0"
    sumd += int(temp)
    print(sumd)
    print(sumd/ count)  
if __name__ == '__main__':
    string1=input("Enter the string:\n")
    sum_and_average(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