Answer to Question #179120 in Python for sai

Question #179120

Given a string, write a program to return the sum and average of the numbers that appear in the string, ignoring all other characters.


1
Expert's answer
2021-04-07T17:41:18-0400
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))

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