Answer to Question #305083 in Python for gopi

Question #305083

numbers in string 2 :

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

but if my input is Anjali25 is python4 the output should be sum of numbers 29 , avg of the numbers 14.5

how to write the program for this input Anjali25 is python4... have to check character by character...


1
Expert's answer
2022-03-02T14:09:58-0500
string = input()
s = 0
c = 0
num = False
tmp = ""
for char in string + " ":
	if char.isdigit():
		tmp += char
		num = True
	else:
		if num:
			c += 1
			s += int(tmp)
			tmp = ""
			num = False
if c != 0:
	print(f"sum of numbers {s}, avg of the numbers {s/c}")

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