3
number1 = int(input('Number #1: '))
number2 = int(input('Number #2: '))
l = len(str(number1))
l1 = len(str(number2))
print()

def addition():
    print(' ',max(number1,number2))
    print('+')
    print(' ',min(number1,number2))
    print('-'*(max(l,l1)+2))
    print('  ')
    print(' ',number1+number2)

def carries():
    while (int(str(number1)[::-1])+int(str(number2)[::-1]))>=10:
        carries = 0
        carries = carries + 1     
        return carries

addition()


print()
print('Carries : ',carries())

I am trying to make a program that does the addition of two user input numbers and calculates the answer while also stating how many carries there are. Carries being if 9+8=17, then there would be 1 carry and so forth. I am having issues with having my program go beyond 1 carry. So this program so far is only applicable for user input numbers that when added are below 99. If you could explain to me how I would go about altering this program to make it applicable to any numbers, that would be great. I was thinking about using the len(number1) and len(number2) and then inputting the string of the user input backwards so it would look like str(number1[::-1])) but I do not think it works like that.

3
  • What exactly do you mean by carry? Commented Mar 2, 2016 at 0:29
  • You also have a return statement inside the while loop; this means the while loop will always have only 1 iteration. If that is what you want, you wouldn't need the while loop. Commented Mar 2, 2016 at 0:29
  • A carry as in: 25+75 = 100 has 2 carries because there are 2 instances in which the addition is greater than 9. 5+5 and 2+7+1(the carry from the previous addition). Commented Mar 2, 2016 at 0:32

3 Answers 3

1

Fun one-liner solution

def numberOfCarryOperations(a, b):
    f=lambda n:sum(map(int,str(n)));return(f(a)+f(b)-f(a+b))/9

# f is the digitSum function :)

Explanation

Asserting a,b >=0, you can proof mathematically: every time you have a carry, the digitSum decreases by 9.

9, because we are in number system 10, so we "lose 10" on one digit if we have carry, and we gain +1 as the carry.

Readable solution

def digitSum(n):
    return sum(map(int,str(n)))

def numberOfCarryOperations(a, b)
    # assert(a >= 0); assert(b >= 0);
    return (digitSum(a) + digitSum(b) - digitSum(a+b)) / 9
Sign up to request clarification or add additional context in comments.

Comments

0

I rewrote your carry function so it works, but the implementation is totally different. You first make the numbers strings so you can iterate through them. Then you make them equal length by appending 0's, and loop through each digit to check whether their sum (plus the carry) is over 9. If it is, increment the counter. Hope this helps:

number1 = int(input('Number #1: '))
number2 = int(input('Number #2: '))
l = len(str(number1))
l1 = len(str(number2))
print()
def addition():
    print(' ',max(number1,number2))
    print('+')
    print(' ',min(number1,number2))
    print('-'*(max(l,l1)+2))
    print('  ')
    print(' ',number1+number2)

def carries():
    num1 = str(number1)
    num2 = str(number2)
    carry = 0
    carries = 0
    c1 = l
    c2 = l
    if (l < l1):
        while (c1 < l1):
            num1 = '0' + num1
            c1+=1
    if (l1 < l):
        while (c2 < l):
            num2 = '0' + num2
            c2+=1
    i = c1
    while (i > 0):
        if (int(num1[i-1])+int(num2[i-1])+carry > 9):
            carry = 1;
            carries+=1
        else:
            carry = 0
        i-=1
    return carries
addition()


print()
print('Carries : ',carries())

Edited with quick fix

5 Comments

Could you explain what c1 and c2 are? I appreciate the help so much. I just want to not copy and paste and understand what I'm programming. Also, in your while loop where "while (i>0):..", just for clarity, I can make an integer indexed? If you don't understand my wording, I apologize, I'm just confused because I was taught that you could only index strings .
Could you also explain as to the meaning or execution of "num1 = '0' + num1"? I'm just confused as to why we are adding the string of 0 to num1.
Yes, c1 and c2 are the new sizes of each number (the size with the concatenated 0s). In the while loop, we index a string. That is why right at the beginning of the function I convert number1 into a string with str()
I chose to concatenate the 0 to the smaller number. Think of what will happen if you try to add 999 and 1. If you only loop through the one's digit, you will forget to carry 2 more times. So I add 0s to make it 999+001, in which case we take care of every digit
If I answered your question, you should mark the answer as correct so others can see the solution. Thanks.
0

Your while loop is fatally flawed.

  • You set carries to 0 every time, and then add 1, so there's no way to return anything but 0.
  • The return is inside the loop, so you will always return after the first iteration.
  • If the ones digits don't provide a carry, then you never get into the loop, and return None.
  • You don't provide for multiple carries, such as the three carries in 999 + 1.
  • If you remove the return from the while loop, you have an infinite loop: the terms of the condition never change. You don't iterate through anything.
  • You gave a function and a variable the same name. This is not good practice.

Here's a start for you, based loosely on your original routine.

  • Set the count of carries to 0.
  • Convert the two numbers to strings and find the common (shorter) length.
  • So long as you have digits in both numbers, grab the digits (moving from the right end) and see whether their numeric sum requires a carry. If so, increment the count.

    def carries(): carry_count = 0 str1 = str(number1) str2 = str(number2)

    for digit_pos in range(1, min(len(str1), len(str2)) + 1):
        if int(str1[-digit_pos]) + int(str2[-digit_pos]) >= 10:
            carry_count += 1
    return carry_count
    

Sample output:

Number #1: 77077
Number #2: 4444

77077
+
4444
-------

81521

Carries : 3

This still has deficiencies for you to fix. Most notably, it doesn't handle multiple carries. However, it should get you moving toward a solution.

Comments

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.