Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Sum Numbers by delahere
def sum_numbers(text: str) -> int:
# your code here
# The string consists of words separated by spaces.
# Split the string into words.
# Initialize the total to zero.
# Step through each word in a for loop. If the word is numeric,
# convert it to an integer and add it to the total.
# When all words have been processed, return the total.
words = text.split()
total = 0
for word in words:
if word.isnumeric():
total += int(word)
return total
print("Example:")
print(sum_numbers("hi"))
# These "asserts" are used for self-checking
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("The mission is done! Click 'Check Solution' to earn rewards!")
April 16, 2023