0

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

1
  • Sounds like your method is misnamed then - Is that function doing something of any use? It seems to be doing a fairly arbitrary operation on the input, Commented Oct 25, 2020 at 3:23

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

yeah we can do that but the requirement is that sum should be done in the function above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.