Ask Question

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 2, 5, 1, and 4. Think about loops: how many times do we loop

+2
Answers (1)
  1. 31 January, 21:48
    0
    def sum_digits (digits):

    total = 0

    for digit in digits:

    total + = int (digit)

    return total

    digits = input ("Enter the digits: ")

    print (sum_digits (digits))

    Explanation:

    Create a method called sum_digits that takes one parameter, digits

    Inside the method, initialize the total as 0. Create a for loop that iterates through the digits. Inside the loop, add the value of the each digit to the total. When the loop is done, return the total

    Ask the user to enter the digits

    Call the method with given digits and print the result
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a program that asks the user to enter a series of single-digit numbers with nothing separating them. The program should display the ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers