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.
print("Input the string:")
s1 = input()
temp = "0"
sum1 = 0
count=0
for c in s1:
if (c.isdigit()):
temp += c
else:
if temp!="0":
count+=1
sum1 += int(temp)
temp = "0"
if s1[-1].isdigit():
count+=1
sum1+= int(temp)
print(sum1)
print(sum1 / count)
Comments
Leave a comment