How to loop a function def in python until I write the number 0

Viewed 245

I'm trying to do a def function and have it add the digits of any number entered and stop when I type the number "0", for example:

Enter the number: 25 Sum of digits: 7

Enter the number: 38 Sum of digits: 11

Enter the number: 0 loop finished

I have created the code for the sum of digits of the entered number, but when the program finishes adding, the cycle is over, but what I am looking for is to ask again for another number until finally when I enter the number "0" the cycle ends :(

This is my code:

def  sum_dig():
 s=0
 num = int(input("Enter a number: "))
 while num != 0 and num>0:
  r=num%10
  s=s+r
  num=num//10
 print("The sum of the digits is:",s)
 if num>0:
  return num
sum_dig()
4 Answers

In order to get continuous input, you can use while True and add your condition of break which is if num == 0 in this case.

def  sum_dig():
    while True:
        s = 0
        num = int(input("Enter a number: "))

        # Break condition
        if num == 0:
            print('loop finished')
            break

        while num > 0:
            r=num%10
            s=s+r
            num=num//10
        print("The sum of the digits is:",s)

sum_dig()

A better approach would be to have sum_dig take in the number for which you want to sum the digits as a parameter, and then have a while loop that takes care of getting the user input, converting it to a number, and calling the sum_digit function.

def sum_dig(num): # takes in the number as a parameter (assumed to be non-zero)
    s=0
    while num > 0: # equivalent to num != 0 and num > 0
        r = num % 10
        s = s + r
        num = num // 10

    return s

while True:
    num = int(input("Enter a number: "))
    if num == 0:
        break

    print("The sum of the digits is: " + sum_dig(num))

This enables your code to adhere to the Single-Responsibility Principle, wherein each unit of code has a single responsibility. Here, the function is responsible for taking an input number and returning the sum of its digits (as indicated by its name), and the loop is responsible for continuously reading in user input, casting it, checking that it is not the exit value (0), and then calling the processing function on the input and printing its output.

Use list() to break the input number (as a string) into a list of digits, and sum them using a list comprehension. Use while True to make an infinite loop, and exit it using return. Print the sum of digits using f-strings or formatted string literals:

def sum_dig():
    while True:
        num = input("Enter a number: ")
        if int(num) <= 0:
            return
        s = sum([int(d) for d in list(num)]) 
        print(f'The sum of the digits is: {s}')

sum_dig()

Rustam Garayev's answer surely solves the problem but as an alternative (since I thought that you were also trying to create it in a recursive way), consider this very similar (recursive) version:

def sum_dig():
    s=0
    num = int(input("Enter a number: "))
    if not num: # == 0
        return num

    while num>0:
        r= num %10
        s= s+r
        num= num//10

    print("The sum of the digits is:",s)
    sum_dig()
Related