7
\$\begingroup\$

Built my first script and its function is to solve simple derivative questions. How can I improve this code? This is my first time posting.

#This is Simple Derivative Solver
 def derivative():
     print("This program will give you the first derivative.")
     input("Press ENTER to continue...")
     cf=int(input("Enter the coefficient of the term..."))
     exp=int(input("Enter the exponent of the term..."))
     if exp==1 and cf==1:
        print("The answer is", 1)
     elif exp<0 and cf<0:
        print("The answer is:", abs(exp*cf), "x^", (exp-1))
     elif exp>0:
        print("The answer is:", exp*cf, "x^", (exp-1))
     elif exp==0:
        print("The answer is:", 0)
     elif exp<0:
        print("The answer is:", abs(exp*cf), "x^", (exp-1))
     elif cf==0:
        print("The answer is:", 0)
     elif cf<0:
        print("THe answer is:", cf*exp, "x^", (exp-1))
derivative()
\$\endgroup\$
1
  • \$\begingroup\$ One other thing that is generally a good thing to do is to separate such function into pure function and side-effecting one that uses it. This will simplify testing, readability, memoization etc. \$\endgroup\$ May 3, 2017 at 18:34

2 Answers 2

8
\$\begingroup\$
  • Trust the math. If exp<0 and cf<0, their product will be positive, so abs is redundant. BTW, in case of elif exp < 0, call to abs leads to an incorrect answer.

  • In any case, \$(ax^n)'\$ always equals to \$anx^{n-1}\$. Mathematically speaking there are no special cases. There are special cases when it comes to printing:

    • \$n = 0\$: you (correctly) print 0

    • \$n = 1\$: you print x^0, but it is better to omit it altogether

    • \$n = 2\$: you print x^1, but it is better to print just x.

    • \$n < 0\$: consider printing parenthesis around the exponent.

  • Separate concerns. The derivative shall only compute the derivative. Input shall be handled separately.

  • The call to derivative should be conditionalized:

    if __name__ == `__main__`:
        derivative()
    

    This enables you to import your code in another project.

\$\endgroup\$
1
\$\begingroup\$

One other thing that is generally a good thing to do is to separate such function into pure function and side-effecting one that uses it. This will simplify testing, readability, memoization etc. Consider this alternative:

def derivative(coefficient, exponent):
    if exp == 1 and cf == 1:
        return 1,
    elif exp < 0 and cf < 0:
        return abs(exp * cf), 'x^', exp - 1
    elif exp > 0:
        return exp * cf, 'x^', (exp-1)
    elif exp == 0:
        return 0
    elif exp < 0:
        return abs(exp * cf), 'x^', exp - 1
    elif cf == 0:
        return 0
    elif cf < 0:
        return cf * exp, 'x^', exp - 1


if __name__ == '__main__':
    print('This program will give you the first derivative.')
    input('Press ENTER to continue...')
    cf = int(input('Enter the coefficient of the term...'))
    exp = int(input('Enter the exponent of the term...'))
    derivative(cf, exp)
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Please provide a code review, not a code dump. \$\endgroup\$
    – anon
    May 3, 2017 at 20:11
  • \$\begingroup\$ @janos I've justified it in my comment. Wether its helpful is for OP to decide. You can delete it or do anything with it, I don't care. I just wanted to help the OP. \$\endgroup\$ May 3, 2017 at 20:59
  • \$\begingroup\$ @user1685095 I overlooked your comment, as it's far away from your post. You should include all relevant information in your post. Having that sentence here makes a big difference, I removed the post notice now. \$\endgroup\$
    – janos
    May 3, 2017 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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