Simple solution in Clear category for Sum Numbers by Natalia99
def sum_numbers(text: str) -> int:
# your code here
lst = text.split() # split text into words
result = 0 # sum of numbers in text
for word in lst: # for each word
try: # If possible to convert word into integer,
i = int(word) # it's mean this consist only from numbers
result += i # so add number to result.
except: # If exception has raised,
pass # go to next word in lst
if __name__ == '__main__':
# These "asserts" are used for self-checking and not for an auto-testing
assert sum_numbers('hi') == 0
assert sum_numbers('who is 1st here') == 0
assert sum_numbers('my numbers is 2') == 2
assert sum_numbers('This picture is an oil on canvas '
'painting by Danish artist Anna '
'Petersen between 1845 and 1910 year') == 3755
assert sum_numbers('5 plus 6 is') == 11
assert sum_numbers('') == 0
print("Coding complete? Click 'Check' to earn cool rewards!")
April 3, 2020