Python: Sum the Digits in an Integer

less than 1 minute read

The code has been modified slightly and now uploaded to the site.

The questions are from the C++ Textbook.

Question

<— Return Home

26.py

value = int(input("Enter a number between 0 and 1000: "))
total = 0
# for (i = len(str(value)) - 1; i >= 0; i--){
startLen = len(str(value)) - 1
for i in range(startLen, -1, -1):
    tmp = (str(value))[i]
    total += int(tmp)
print("The sum of the digits is " + str(total))

<— Return Home