Ask Question
16 May, 01:12

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

+5
Answers (1)
  1. 16 May, 01:53
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers