Primary arithmetic. Write a program (called q that adds two numbers and counts t
ID: 3803107 • Letter: P
Question
Primary arithmetic. Write a program (called q that adds two numbers and counts the number of carry operations. You will define your own user-defined functions for this problem. Your solution must make use of at least two user-defined functions to receive full credit Programming tips. There are a number of different ways to tackle this problem. Consider first working on printing out the addition equation as shown in the following examples. Then, work on computing the number of carries required for the addition. Finally, a built-in function that you may find useful to incorporate in your program is max For example, to find the maximum of 3 numbers a4, 198, and -3, use the max (54, 198 34), which returns 198.Explanation / Answer
number1 =input('enter number1')
number2 =input('enter number2')
def check_length(number1,number2):
if len(number1) == len(number2):
pair_list = zip(number1,number2)
pair_list.reverse()
return pair_list
else:
if len(number1) > len(number2):
dif = len(number1) - len(number2)
number2 = '0'*dif + number2
else:
dif = len(number2) - len(number1)
number1 = '0'*dif + number1
pair_list = zip(number1, number2)
pair_list.reverse()
return pair_list
def carry_count (pair_list):
count = 0
result = 0
for i,j in pair_list:
if int(i) + int(j) + int(count) > 9:
count = 0
result = result + 1
count = str(int(i) + int(j))[0]
return result
zip_list = check_length(str(number1),str(number2))
output = carry_count(zip_list)
print 'carry count is ',output
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.