# This is my first code in python. (So, the code is too dirty.) # I don't know that this calculator works exactly. # +----------------------+ # | This is a calculator | # +----------------------+ # # Enter the calculate formula # # => 1 + 3 / 4 # # The answer is: 1.75 class colors: OKBLUE = '\033[94m' OKCYAN = '\033[96m' OKGREEN = '\033[92m' WARNING = '\033[93m' ENDC = '\033[0m' BOLD = '\033[1m' def calculator(): print("\n+----------------------+") print("| " + colors.WARNING + "This is a calculator" + colors.ENDC + " |") print("+----------------------+\n") formula = (input("Enter the calculate formula\n\n=> "+ colors.OKCYAN)).split(" ") array = [] for i in range(len(formula)): if (formula[i] == "x"): array.append(i) elif (formula[i] == "/"): array.append(i) n = 0 for i in range(len(array)): r = 0 if (formula[array[i] - n] == "x"): r = float(formula[array[i] - 1])*float(formula[array[i] + 1]) formula.pop(array[i] - 1 - n) formula.pop(array[i] - 1 - n) formula.pop(array[i] - 1 - n) formula.insert(array[i] - 1 - n, r) n = n + 2 elif (formula[array[i] - n] == "/"): r = float(formula[array[i] - 1 - n])/float(formula[array[i] + 1 - n]) formula.pop(array[i] - 1 - n) formula.pop(array[i] - 1 - n) formula.pop(array[i] - 1 - n) formula.insert(array[i] - 1 - n, r) n = n + 2 array = [] for i in range(len(formula)): if (formula[i] == "+"): array.append(i) elif (formula[i] == "-"): array.append(i) n = 0 for i in range(len(array)): r = 0 if (formula[array[i] - n] == "+"): r = float(formula[array[i] - 1 - n])+float(formula[array[i] + 1 - n]) formula.pop(array[i] - 1 - n) formula.pop(array[i] - 1 - n) formula.pop(array[i] - 1 - n) formula.insert(array[i] - 1 - n, r) n = n + 2 elif (formula[array[i] - n] == "-"): r = float(formula[array[i] - 1 - n])-float(formula[array[i] + 1 - n]) formula.pop(array[i] - 1 - n) formula.pop(array[i] - 1 - n) formula.pop(array[i] - 1 - n) formula.insert(array[i] - 1 - n, r) n = n + 2 print(colors.OKGREEN + "\nThe answer is:" + colors.BOLD, formula[0], colors.ENDC, "\n") calculator()