binary calculator in python

Solutions on MaxInterview for binary calculator in python by the best coders in the world

showing results for - "binary calculator in python"
Silvia
02 Oct 2019
1#this is a binary calculator
2#calculates result in binary and gives answer in decimal...
3#enjoy
4import time
5
6def intro():
7    print("This is a calculator which uses binary to calculate")
8
9
10def take_n_feed_command():
11    print("type 'c' to switch to complex calculator ")
12    print("Type the formula (include correct spaces between operators):-")
13    q = str(input(""))
14    if q == "c":
15        complexcal()
16    else:
17        if "+" in q:
18            q = q.replace("+ ","")
19            (q1,q2) = q.split()
20            print()
21            print("This was delivered to our system:")
22            print(q1,q2)
23            q1 = int(q1)
24            b1 = bin(q1)
25            q2 = int(q2)
26            b2 = bin(q2)
27            print()
28            print("Binary form converted:")
29            print(b1.replace("0b",''),b2.replace("0b",''))
30            print()
31            print("calculating...")
32            print("done!")
33            print()
34            time.sleep(0.3)
35            integer_sum = int(b1, 2) + int(b2, 2)
36            print("calclated result: ",bin(integer_sum))
37            print()
38            print("imterpreting it to decimal...")
39            time.sleep(1)
40
41            print("done!")
42            print()
43            print()
44            print("the answer is : ", integer_sum)
45        elif "-" in q:
46            q = q.replace("- ", "")
47            (q1, q2) = q.split()
48            print()
49            time.sleep(1)
50            print("This was delivered to our system:")
51            print(q1, q2)
52            q1 = int(q1)
53            b1 = bin(q1)
54            q2 = int(q2)
55            b2 = bin(q2)
56            print()
57            time.sleep(500)
58            print("Binary form converted:")
59            print(b1.replace("0b", ''), b2.replace("0b", ''))
60            print()
61            print("calculating...")
62            time.sleep(1000)
63            print("done!")
64            print()
65            time.sleep(500)
66            integer_sum = int(b1, 2) - int(b2, 2)
67            print("calclated result: ", bin(integer_sum))
68            print()
69            print("imterpreting it to decimal...")
70            time.sleep(1000)
71            print("done!")
72            print()
73            print()
74            print("the answer is : ", integer_sum)
75        repeat()
76
77def repeat():
78    take_n_feed_command()
79
80def complexcal():
81    print()
82    print('...')
83    print("Type the complex formulae (eg. 12 - 78 * 56 / 67 * 328):")
84    f = input()
85    a = eval(f)
86    print()
87    print(a)
88    print()
89    repeat()
90
91if __name__ == "__main__":
92    intro()
93    take_n_feed_command()
94
95
96
97