Answer to Question #186153 in Python for manikanta

Question #186153

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


1
Expert's answer
2021-04-30T16:52:21-0400
def sum_of_digits(s):
	'''
	this function that takes a string and returns
	the sum of the digits in the string
	and the average of the digits
	'''
	k = 0
	digit_sum = 0
	digit_average = 0
	for char in s:
		if char.isdigit():
			k += 1
			digit_sum += int(char)
	if k > 0:
		digit_average = round((digit_sum / k),2)
	return digit_sum, digit_average


while True:
	s = input()
	if s == '':
		break
	print(*sum_of_digits(s), sep='\n')

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