Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Jupyter notebook assignments/Week2/quadratic_roots.ipynb

173 views
Kernel: Python 2 (SageMath)
# Compute the smaller root of a quadratic equation. ################################################### # Smaller quadratic root formula # Student should enter function on the next lines. import math a=0 b=0 c=0 d=b^2 - 4*a*c if d>0: x = (-b+math.sqrt(b^2 - 4*a*c))/2*a y = (-b-math.sqrt(b^2 - 4*a*c))/2*a if x>y: smaller_root=y else: smaller_root=x elif d==0: z= (-b+math.sqrt(b^2 - 4*a*c))/2*a smaller_root=z else: smaller_root=input("Error") ################################################### # Tests # Student should not change this code. def test(a, b, c): """Tests the smaller_root function.""" print "The smaller root of " + str(a) + "x^2 + " + str(b) + "x + " + str(c) + " is:" print str(smaller_root(a, b, c)) test(1, 2, 3) test(2, 0, -10) test(6, -3, 5) ################################################### # Expected output # Student should look at the following comments and compare to printed output. #The smaller root of 1x^2 + 2x + 3 is: #Error: No real solutions #None #The smaller root of 2x^2 + 0x + -10 is: #-2.2360679775 #The smaller root of 6x^2 + -3x + 5 is: #Error: No real solutions #None
The smaller root of 1x^2 + 2x + 3 is:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-3-739f02b92059> in <module>() 36 37 ---> 38 test(1, 2, 3) 39 test(2, 0, -10) 40 test(6, -3, 5) <ipython-input-3-739f02b92059> in test(a, b, c) 33 34 print "The smaller root of " + str(a) + "x^2 + " + str(b) + "x + " + str(c) + " is:" ---> 35 print str(smaller_root(a, b, c)) 36 37 TypeError: 'float' object is not callable
smaller quadratic root formula=(x=(-b)) def test(a, b, c): """Tests the smaller_root function.""" print"The smaller root of " +str(a) + "x^2 +" + str(b) +"x + " + str(c) + "is:" print str(smaller_root(a, b, c)) test(2, 0, -10)
The smaller root of 2x^2 +0x + -10is:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-6-730242505113> in <module>() 6 7 ----> 8 test(2, 0, -10) <ipython-input-6-730242505113> in test(a, b, c) 3 4 print"The smaller root of " +str(a) + "x^2 +" + str(b) +"x + " + str(c) + "is:" ----> 5 print str(smaller_root(a, b, c)) 6 7 NameError: global name 'smaller_root' is not defined