Answer to Question #171149 in Python for Radhika

Question #171149

Numbers in String - 2

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.Explanation


For example, if the given string is "I am 25 years and 10 months old", the numbers are 25, 10. Your code should print the sum of the numbers(35) and the average of the numbers(17.5) in the new line.


1
Expert's answer
2021-03-13T08:24:35-0500
# input the string
p = input()
# storing the digits in the list
lst = []


sum = 0
for digit in p:
    if digit.isdigit ():
        lst.append(int(digit))
        sum = sum + int(digit)
print("numbers are :", lst )
print("Sum of the numbers is:",sum)
print("Average value of numbers is:", sum/len(lst))

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

Radhika
12.03.21, 16:48

i want sum and average for this code. you give only sum.

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS