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?
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)))
Comments
Leave a comment