Answer to Question #176356 in Python for raghu

Question #176356

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.


1
Expert's answer
2021-03-28T08:47:12-0400

# 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))


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