Divide the input integers and add the sum of each digit in the integers

Asked 2 years ago, Updated 2 years ago, 14 views

Hello, I wrote the code in Python, but I got a compilation error, so I'm asking you a question.

Please explain which part is wrong

Problem: Split the input integers and write a code that adds the sum of each digit in the integer. Example: input: 1234, output: 10

// Enter code here
n = int("Enter an integer":")) 
a = str(n)
b = len(a)
c = 0
for i in range(0,b+1):
    d = a[i]
    e = int(d)
    c = c+e
print(c)

python

2022-09-20 10:51

1 Answers

Good job.

One problem is that in an array or list with length b, the index is up to b-1.


2022-09-20 10:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.