smith
smith

Reputation: 1

Calling and addition function in python

is there any way that I could add the string of one function by calling from other function.

def sumOfDoubleEvenPlace(number):
    sum += (getDigit(number))  # sum should be in this function 

this is calling function.

def getDigit(number)-> int:
    sum = 0
    for i in range (len(number)-2, -1, -2):
        digit = int(number[i])
        digit = digit * 2
        if (digit >= 10):
        digit = (digit % 10) + 1
        #sum += digit   this is the result i want in the above function
    return digit 

this function returns digit, and i want to add all the digits from get digit function into sum function. I commented the line, sum. So i want this sum in the first function. thanks for looking

Upvotes: 0

Views: 98

Answers (1)

wasif
wasif

Reputation: 15480

Instead return the sum:

def getDigit(number)-> int:
    sum = 0
    for i in range (len(number)-2, -1, -2):
        digit = int(number[i])
        digit = digit * 2
        if (digit >= 10):
          digit = (digit % 10) + 1
          sum += digit
    
    return sum

In your case, we can yield digits first:

def getDigit(number)-> int:
    sum = 0
    for i in range (len(number)-2, -1, -2):
        digit = int(number[i])
        digit = digit * 2
        if (digit >= 10):
          digit = (digit % 10) + 1
          yield digit

And use for loop to calculate sum:

for x in getDigit(NUMBER):
  sum += x

Upvotes: 1

Related Questions