I\'m getting this error message found in the code. What am I doing wrong? How do
ID: 3592334 • Letter: I
Question
I'm getting this error message found in the code. What am I doing wrong? How do I fix this?
Here is the original question:
Python Question: For the following use a For loop. Write a function MakeSubintF (a, b, n) that returns a list of the endpoints of the subintervals, e.g. MakeSubint (7, 10, 3) should return the list [7, 8, 9, 10]. Verify that your function works with the intervals (a,b) = (7,10) (n=3); (12,19.5) (n=10); and (0,2pi) (n=8).
My code:
def MakeSubintF(anD'n): # defines the function MakesubIntF(a,b,n) that returns a list of endpoints length = (b-a)/n # defines length of subintervals i- a # defines the starting point necessary for the following conditional statement results = [] # sets the list of results for i in range (a,b); results.append(i) # adds 1 to the end of results 1-1 + length # sets the start point for the loop return results # list of endpoints ranging from a to b import math # imports the math library used for pi rl -MakesubintF (7,10,3) # defines the list of endpoints for r1 r2 = MakeSub IntF( 12, 19.5, 10) # defines the list of endpoints for r2 r3 = MakeSubintF(0,2-math.pl, 8) # defines the list of endpoints for r3 print (r1) # prints r1 print (r2) # prints r2 print (r3 ) # prints r3 Traceback (most recent call last) TypeError Kipython-input-17-4a919bcc18f0> in 9 import math # imports the math library used for pi 10 r1 = MakeSubintF (7,10, 3) # defines the list of endpoints for r1 > 11 r2 = MakesubintF (12, 19.5, 10) # defines the list of endpoints for r2 12 r3 -MakeSubintF (0,2 *math. p1,8) # defines the list of endpoints for r3 13 print (r1) # prints r1Explanation / Answer
The mistake is that range() accepts only integer end arguments, but you have passed a float argument in the MakeSubintF(12, 19.5, 10).
So it's wrong to use a range there.
Instead you can use a while loop for iterating like the below code.
code :
def MakeSubintF(a, b, n):
length = (b-a)/n
i = a;
results = []
while i < b:
results.append(i)
i = i + length
return results
import math
r1 = MakeSubintF(7, 10, 3)
r2 = MakeSubintF(12,19.5,10)
r3 = MakeSubintF(0,2*math.pi,8)
print (r1)
print (r2)
print (r3)
# sorry that indentation is not there
# this editor doesn't support indentation
# please indent the code before executing
# if any queries you can comment
# thank you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.