Question #191278

Numbers in String - 1
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.
Sample Input 1
I am 25 years and 10 months old
Sample Output 1
8
2.0

Sample Input 2
Tech Foundation 35567
Sample Output 2
26
5.2

Sample Input 3
Anjali25 is python4 Expert
Sample Output 3
11
3.67

Expert's answer

def sum_and_average(string):
	
	count = 0
	sum_of_digits = 0
	for char in string:
		if char.isdigit():
			count += 1
			sum_of_digits += int(char)
	if count > 0:
		average = round((sum_of_digits / count),2)
	else:
		average = 0
	print(sum_of_digits, average, sep='\n')


while True:


	s = input()
	sum_and_average(s)
LATEST TUTORIALS
APPROVED BY CLIENTS