# A basic calculator in Python

This is a simple calculator I created using my Python basic knowledge. I would like to know any coding techniques to simplify and enhance the code to make it efficient. I would also like to know some advanced techniques. Also, please specify where I can reduce duplication to make it DRY.

import math
import cmath
print("""Choose from
1. Perform basic BODMAS
2. Find squareroot
4. Find Trignometric values
5. Find logarithms""")
choose = input("enter required number \n")

if choose == '1':

def bodmas():
num1 = float(input("Enter number 1 \n"))
num2 = float(input("Enter number 2 \n"))
res = num1+num2
elif op == "sub":
res = num1 - num2
elif op == "mul":
res = num1*num2
elif op == "div":
res = num1/ num2
else:
res = print("Error")
return res
print(bodmas())

elif choose == '2':

def sqrt_n():
num1 = float(input("Enter number"))
res = math.sqrt(num1)
return res
print(sqrt_n())

elif choose =='3':

print("Standard form of Quadratic equation (Ax^2 + Bx + C)")
A = int(input("Enter coefficient A: "))
B = int(input("Enter coefficient B: "))
C = int(input("Enter coefficient C: "))
D = (B**2)-(4*A*C)  # Calculate Discriminant

sol1 = (-B-cmath.sqrt(D))/(2*A)
sol2 = (-B+cmath.sqrt(D))/(2*A)

z =print("The solutions are {0} and {1}" .format(sol1 ,sol2))
return z
elif choose == '4':

choice = input("Choose sin,cos,tan: ")
if choice == "sin":
ang = float(input("Enter angle: "))
print (x)
elif choice == "cos":
ang = float(input("Enter angle: "))
print (x)
elif choice == "tan":
ang = float(input("Enter angle: "))
print (x)
else:
print("Error")

elif choose == "5":
z = int(input("Enter Value: "))
res = math.log10(z)
print (res)
else
print("Error")


How can I use the functions in a more preferable and efficient way?

• I think you want to implement DRY (Don't Repeat Yourself), not avoid it. I made some minor modifications to clear up your question. Welcome to Code Review, I hope you get some great answers. If I wouldn't be short on time I'd write one myself.
– Mast
Aug 14, 2017 at 9:35
• Possibly split into functions, where each function does one task. Aug 14, 2017 at 9:38

Welcome to Code Review :) Here are some nits and tips to get you on your way.

1. It is much more Pythonic to define your functions outside the user flow and put the user flow inside of a main() function. The flow should look like this:

def bodmas():
...

def sqrt_n():
...

def main():
print("""Choose from
...
""")
choose = input("enter required number \n")
if choose == '1':
...
bodmas()
...
elif choose == '2':
...

2. Use an if __name__ == '__main__': guard to invoke your main() function.

if __name__ == '__main__':
main()

3. Do you want to handle and catch errors?

A = int(input("Enter coefficient A: "))
...
sol1 = (-B-cmath.sqrt(D))/(2*A)


What if the user enters a non-numeric coefficient? Or what if the user enters 0? This is also applicable if the user tries to divide by zero in the bodmas() function.

4. Consider following a style guide like PEP8 to make your code more readable and consistent.

Happy coding!