Answers for "binary calculator in python"

0

binary calculator in python

#this is a binary calculator
#calculates result in binary and gives answer in decimal...
#enjoy
import time

def intro():
    print("This is a calculator which uses binary to calculate")


def take_n_feed_command():
    print("type 'c' to switch to complex calculator ")
    print("Type the formula (include correct spaces between operators):-")
    q = str(input(""))
    if q == "c":
        complexcal()
    else:
        if "+" in q:
            q = q.replace("+ ","")
            (q1,q2) = q.split()
            print()
            print("This was delivered to our system:")
            print(q1,q2)
            q1 = int(q1)
            b1 = bin(q1)
            q2 = int(q2)
            b2 = bin(q2)
            print()
            print("Binary form converted:")
            print(b1.replace("0b",''),b2.replace("0b",''))
            print()
            print("calculating...")
            print("done!")
            print()
            time.sleep(0.3)
            integer_sum = int(b1, 2) + int(b2, 2)
            print("calclated result: ",bin(integer_sum))
            print()
            print("imterpreting it to decimal...")
            time.sleep(1)

            print("done!")
            print()
            print()
            print("the answer is : ", integer_sum)
        elif "-" in q:
            q = q.replace("- ", "")
            (q1, q2) = q.split()
            print()
            time.sleep(1)
            print("This was delivered to our system:")
            print(q1, q2)
            q1 = int(q1)
            b1 = bin(q1)
            q2 = int(q2)
            b2 = bin(q2)
            print()
            time.sleep(500)
            print("Binary form converted:")
            print(b1.replace("0b", ''), b2.replace("0b", ''))
            print()
            print("calculating...")
            time.sleep(1000)
            print("done!")
            print()
            time.sleep(500)
            integer_sum = int(b1, 2) - int(b2, 2)
            print("calclated result: ", bin(integer_sum))
            print()
            print("imterpreting it to decimal...")
            time.sleep(1000)
            print("done!")
            print()
            print()
            print("the answer is : ", integer_sum)
        repeat()

def repeat():
    take_n_feed_command()

def complexcal():
    print()
    print('...')
    print("Type the complex formulae (eg. 12 - 78 * 56 / 67 * 328):")
    f = input()
    a = eval(f)
    print()
    print(a)
    print()
    repeat()

if __name__ == "__main__":
    intro()
    take_n_feed_command()
Posted by: Guest on August-06-2021

Python Answers by Framework

Browse Popular Code Answers by Language