Answer to Question #187169 in Python for Hari nadh babu

Question #187169

Given a string, write a program to return the sum and average of the digits of all 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 digits of all 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 digits of all numbers that appear in the string are 2, 5, 1, 0. Your code should print the sum of all digits(8) and the average of all digits(2.0) in the new line.


Input 1:-

I am 25 years and 10 months old

Output 1:-

8

2.0


Input 2:-

Tech Foundation 35567

Output 2:-

26

5.2


Input 3:-

Anjali25 is python4 Expert

Output 3:-

11

3.67


When we give three Inputs one by one they can give exact three Outputs must be come when code can be executed


1
Expert's answer
2021-05-01T08:17:31-0400
print('Enter the string with number')
inString =input("");
sumOfDigits =0
numberOfDigits=0


for character in inString:
    if (character.isdigit()):
        sumOfDigits+=int(character)
        numberOfDigits+=1

average=sumOfDigits/numberOfDigits

print(str(sumOfDigits))

print("%.1f" % average)

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