Given a string, write a program to return the sum and average of the numbers that appear in the string, ignoring all other characters.
str = input("Enter string: ")
l = len(str)
nums = []
i = 0
while i < l:
s_int = ''
a = str[i]
while '0' <= a <= '9':
s_int += a
i += 1
if i < l:
a = str[i]
else:
break
i += 1
if s_int != '':
nums.append(int(s_int))
sum = 0
for i in range(len(nums)):
sum += nums[i]
print("Sum is: ", sum)
print("Average is: ", sum/len(nums))
Comments
Leave a comment