5
\$\begingroup\$

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 
        3. Solve quadratic equation 
        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"))
        op= input("Enter add,sub,mul,div \n")
        if op == "add":
            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':

    def quad_s():
        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
    print(quad_s())
elif choose == '4':

    choice = input("Choose sin,cos,tan: ")
    if choice == "sin":
        ang = float(input("Enter angle: "))
        x = math.sin(math.radians(ang))
        print (x)
    elif choice == "cos":
        ang = float(input("Enter angle: "))
        x = math.cos(math.radians(ang))
        print (x)
    elif choice == "tan":
        ang = float(input("Enter angle: "))
        x = math.tan(math.radians(ang))
        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?

\$\endgroup\$
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Mast Aug 14 '17 at 9:35
  • \$\begingroup\$ Possibly split into functions, where each function does one task. \$\endgroup\$ – hjpotter92 Aug 14 '17 at 9:38
5
\$\begingroup\$

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!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.