Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In python - write a program that asks the user to enter a series of single-digit

ID: 3561817 • Letter: I

Question

In python - write a 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 these numbers.

This is what I have so far:

# This program asks the user to enter a series of single-digit numbers

# with nothing seperating them. The program will display the sum of

# all the single digit numbers in the string.

def main():
# Get the list of numbers from the user.
user_list = input('Enter a series of single-digit numbers: ')

# Create a variable to use as an accumulator.
def sum_digits(digit):
sum = 0

# Calculate the sum of the numbers provided by the user.
for num in user_list:
sum += num

# Return the total.
def sum_digits(your_string):
return sum(int(x) for x in your_string if '0' <= x <= '9')

# Show the user the sum of the numbers.
print('The sum of those numbers is ', sum, '.')

# Call the main function.
main()

Explanation / Answer

Save this file into C:Python34includesu_digit.py

def sum_digits(n):
    s = 0
    while n:
        s += n % 10
        n = n//10
    return s

Then use Python Command Panel and type this

>>> import Su_digit
>>> Su_digit.sum_digits(123456)
21