Adding all digits for a positive integer

M-M

I am trying to add all the digits of a positive integer. When I test out the number 434, the result was 4, instead of 11.

Here is what I have, I don't understand why is my code not going through all the digits of the number. How to correct it?

def digit_sum(x):
  n=0
  if x>0:
    #get the end digit of the number,add to n
      n+=x%10
      #remove the end digit of the number
      x=x//10
  return n
paxdiablo

Two main constructs of procedural programming are:

  1. Choosing among different things once. (Selection)
  2. Doing things more that once. (Iteration)

An if statement is a selection statement. Since you want to add up all the digits in the number, your use of if is inappropriate. In other words, using a selection statement will only give you the sum of the first digit you process (the final 4 in 434).

Instead you should use an iteration statement like while:

def digit_sum(number):
  sumOfDigits = 0
  while number > 0:
      sumOfDigits += number % 10
      number = number // 10
  return sumOfDigits

You'll notice I've used more descriptive variable names. That's a good habit to get into because it self-documents your code.


You can also look into more Pythonic ways of doing this, such as:

def digit_sum(number):
    return sum([int(digit) for digit in str(number)])

Whether that can be considered better is open for debate but it's a well-known way of writing Python succinctly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Truncate all digits of an integer except the first two

From Dev

Product of all digits of an integer number C#

From Dev

Adding an integer to all values in a tuple

From Dev

How to write a recursive method in java that takes in a positive or negative integer and returns the number of digits it has

From Dev

Find all combinations of `n` positive numbers adding up to `k` in Python?

From Dev

Generate random integer with ALL digits from 1-9

From Dev

all integers which are subsequences of an integer (considered as a string of digits)

From Dev

all integers which are subsequences of an integer (considered as a string of digits)

From Dev

Optimization with positive integer parameters

From Dev

Matching positive integer with haskell

From Dev

DocumentFilter for negative and positive integer

From Dev

Finding specific positive integer

From Dev

Positive Integer in Velocity Template

From Dev

Validating a string as a positive integer

From Dev

Limit the number of digits in integer?

From Dev

Reverse digits of an integer

From Dev

Sum of the digits of an integer in lua

From Dev

How to reverse digits of integer?

From Dev

Limit the number of digits in integer?

From Dev

Reverse digits of an integer in javascript

From Dev

Break an integer to a list of digits

From Dev

Sorting digits in a string or integer?

From Dev

Drop digits from an integer

From Dev

Counting number of digits in integer

From Dev

Adding values large digits

From Dev

How do I find all positive integer solutions to 7x+2y=n

From Dev

Matrices: Write a function in MATLAB that returns all of the positive integer numbers that satisfy the following property

From Dev

How to print all primes which are not greater than the given positive integer N?

From Dev

PostgreSQL - get all integer digits from double precision field for use in to_char function

Related Related

  1. 1

    Truncate all digits of an integer except the first two

  2. 2

    Product of all digits of an integer number C#

  3. 3

    Adding an integer to all values in a tuple

  4. 4

    How to write a recursive method in java that takes in a positive or negative integer and returns the number of digits it has

  5. 5

    Find all combinations of `n` positive numbers adding up to `k` in Python?

  6. 6

    Generate random integer with ALL digits from 1-9

  7. 7

    all integers which are subsequences of an integer (considered as a string of digits)

  8. 8

    all integers which are subsequences of an integer (considered as a string of digits)

  9. 9

    Optimization with positive integer parameters

  10. 10

    Matching positive integer with haskell

  11. 11

    DocumentFilter for negative and positive integer

  12. 12

    Finding specific positive integer

  13. 13

    Positive Integer in Velocity Template

  14. 14

    Validating a string as a positive integer

  15. 15

    Limit the number of digits in integer?

  16. 16

    Reverse digits of an integer

  17. 17

    Sum of the digits of an integer in lua

  18. 18

    How to reverse digits of integer?

  19. 19

    Limit the number of digits in integer?

  20. 20

    Reverse digits of an integer in javascript

  21. 21

    Break an integer to a list of digits

  22. 22

    Sorting digits in a string or integer?

  23. 23

    Drop digits from an integer

  24. 24

    Counting number of digits in integer

  25. 25

    Adding values large digits

  26. 26

    How do I find all positive integer solutions to 7x+2y=n

  27. 27

    Matrices: Write a function in MATLAB that returns all of the positive integer numbers that satisfy the following property

  28. 28

    How to print all primes which are not greater than the given positive integer N?

  29. 29

    PostgreSQL - get all integer digits from double precision field for use in to_char function

HotTag

Archive