This is the adder method of my binary calculator. I did not cover for the case that someone passes a non binary number into the calculator yet.
I am pretty sure I have covered every other edge case for the calculator. I am trying to learn refactoring and how to look for simpler ways to do things. I know that I have a bunch of if's, but I don't know any other way I could have made the logic for this. Could someone mentor me through how they would refactor my code and why?
class BinaryCalculator: def bit_adder(self, number1, number2): num_a = self.num_to_list(number1) num_b = self.num_to_list(number2) maxx = max(len(num_a), len(num_b)) minn = min(len(num_a), len(num_b)) if minn == len(num_b): smaller = num_b num_b =  * (maxx - minn) for i in smaller: num_b.append(i) else: smaller = num_a num_a =  * (maxx - minn) for i in smaller: num_a.append(i) print " ",''.join(map(str, num_a)) print "+",''.join(map(str, num_b)) print '----------' carry_bit = 0 answer =  * (maxx + 1) for bit in xrange(maxx - 1, -1, -1): sum_bit = 0 if num_a[bit] == num_b[bit]: if num_a[bit] == 1: if carry_bit == 0: carry_bit += 1 else: sum_bit = 1 else: if carry_bit == 1: sum_bit = 1 carry_bit -= 1 else: if carry_bit == 1: sum_bit == 0 else: sum_bit = 1 answer[bit + 1] = sum_bit if carry_bit == 1: answer = 1 print ''.join(map(str, answer)) def num_to_list(self, num): num = str(num) list_to_return =  for bit in range(len(num)): list_to_return.append(int(num[bit])) return list_to_return c = BinaryCalculator() c.bit_adder(10011001, 1001)