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()
-
\$\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\$user1685095– user16850952017年05月03日 18:34:10 +00:00Commented May 3, 2017 at 18:34
2 Answers 2
Trust the math. If
exp<0 and cf<0
, their product will be positive, soabs
is redundant. BTW, in case ofelif exp < 0
, call toabs
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 justx
.\$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.
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)
-
1\$\begingroup\$ Please provide a code review, not a code dump. \$\endgroup\$anon– anon2017年05月03日 20:11:07 +00:00Commented 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\$user1685095– user16850952017年05月03日 20:59:29 +00:00Commented 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– janos2017年05月03日 21:16:35 +00:00Commented May 3, 2017 at 21:16