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...
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}")
Comments
Leave a comment