Answered step by step
Verified Expert Solution
Question
1 Approved Answer
For this exercise you'll write a program called carry.py that prompts the user for two positive integers of any length and adds them together. In
For this exercise you'll write a program called carry.py that prompts the user for two positive integers of any length and adds them together. In addition to adding them, it also counts the number of times the "carry" operation needs to be carried out. For example, in this case, none of the column additions exceed 9, so no digits need to be carried: $ python carry.py Enter the first number: 16 Enter the second number: 21 16 + 21 = 37 Number of carries: 0 In this case, the 5 and the 8 exceed 9, so a 1 is carried into the next column: $ python carry.py Enter the first number: 275 Enter the second number: 18 275 + 18 = 293 Number of carries: 1 In this example, all columns add up to 10 or greater, so there is a carry on each column: $ python carry.py Enter the first number: 981 Enter the second number: 999999 981 + 999999 = 1000980 Number of carries: 6 Hints Counting carries is not something that is built in to Python's integer arithmetic functionality, so you'll have to implement this manually, adding the numbers column by column. To do this, you will want to represent digits in a sequentially accessible way (lists!). It may be helpful to think of the problem in terms of four rows of digits, each represented by a list. The two original numbers are each rows, the solution is a row, and the sequence of carries can be thought of as another row. By using list indices, you can easily access corresponding digits in each of the four relevant rows. String casting can also be useful. If you want to easily break a multi-digit integer into a list of digits, you can cast it first to a string and then to a list (it won't work to try to cast a number straight to a list). For this exercise, you must define at least two functions aside from maino. From now on, try to think about minimizing the amount of functionality you leave to maino. Ideally, main should only be a few lines of code. Break as many operations as you can into their own functions
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started