46.0k views
1 vote
Write a Python program that asks the user to enter a series of single-digit numbers with nothing separating them.

The program should display the sum of all the single-digit numbers in the string.


For example, if the user enters 2514, the method should return 12, which is the sum of 2, 5, 1 and 4.

1 Answer

3 votes

The Python 3 code for the program described in the question:

def sum_digits(str):

sum = 0

for c in str:

sum += int(c)

return sum

def main():

print("Enter series of single-digit numbers with no spaces: ")

str = input()

print("The sum of digits of the entered number is", sum_digits(str))

main()

User Oluremi
by
4.7k points