I'm creating a program that adds each digit number, but I don't know how to correct the error.

Asked 2 years ago, Updated 2 years ago, 52 views

You are creating a program that adds each digit number.

In the first iteration of the function lot, we figure out the number of digits, and in the second iteration, when we put the number 7984 in x, for example, 7984, 798, 79, 7, and in the third iteration, if y is 7984, we subtracted the value of 798 by 10 and added the existing value of y. And if we add all of x[], we're going to get y back, and if y is greater than 10, we're going to do it again with n.

However, the index out of range error is not fixed. Please tell me how to modify it.

error python

2022-09-20 15:53

1 Answers

This is probably the most common Python code that adds up to a number of digits.

digit number = sum(map(int, str(number))))
>>> sum(map(int, str(33)))
6
>>> sum(map(int, str(12345)))
15


2022-09-20 15:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.