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 string from a user
s=input('Input string>')
# set variables for sum and average to 0
sum_digits=0
num_digits=0
# search digits in the string
for ss in s:
if ss.isdigit():
num_digits+=1
sum_digits+=int(ss)
# print results
print('sum of the digits is ' + str(sum_digits))
print('average of the digits is ' + str(sum_digits/num_digits))
Comments
Leave a comment