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

Respuesta :

Answer:

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

The program that asks the user to enter a series of single-digit numbers with nothing separating them and then the program should display the sum of all the single digit numbers in the string can represented below;

x = input("Enter a series of single-digit numbers with nothing separating them:")

y = []

for i in x:

   y += i

z = sum(map(int, y))

print(z)

The code is written in python

We used x variable to ask the user input and store it

An empty array is declared.

We loop through the user input and put the value in the empty array.

we store the sum of the converted strings to integers in the array.

Finally, we print the variable z.

read more: https://brainly.com/question/14234740?referrer=searchResults

Ver imagen vintechnology
Ver imagen vintechnology
ACCESS MORE
EDU ACCESS
Universidad de Mexico