Answer to Question #200342 in Python for sudheer

Question #200342

Given a string, write a program to return the sum and average of the 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 numbers that appear in the string.

Note: Round the average value to two decimal places.

can anyone please give the code for this?


1
Expert's answer
2021-05-31T16:36:45-0400
source = input()
tmp0 = list(source)
tmp1 = [c if c.isdigit() else ' ' for c in tmp0]
tmp2 = "".join(tmp1)
tmp3 = tmp2.split()
numbers = []
for w in tmp3:
    numbers.append(int(w))
sum = 0
for i in numbers:
    sum += i
print('sum = ', sum)
print('avg = ', '{:.2f}'.format(sum/len(numbers)))

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