I\'m creating a program that prints n subintervals of equal length. In the first
ID: 3589107 • Letter: I
Question
I'm creating a program that prints n subintervals of equal length. In the first block of code there's no problem printing the subintervals but my question is how do I fix the second block of code to display 10 subintervals from the interval (12,19.5)?
In [6]: a = 7 # defines the first element of our interval (a,b) b = 10 # defines the last element of our interval (a,b) n = 3 # defines the number of equidistant subintervals that divides (a,b) a # defines a starting point necessary for the following conditional statement while |Explanation / Answer
I guess the formula to arrive at the correct increment value woul be (b-a)/n.
In the first case, you were able to correctly able to produce 10 subintervals at an increment of (10-7)/3 = 1.
In the second case, you need to to produce 10 subintervals betweeen 12 and 19.5, but the increment you're using is 1. This is why it prints (12,13), (13,14) and so on.
The value of the increment should be (19.5-12)/10 = 0.75, i.e., x=i+0.75
--------------------------------------------------------------------------------------------------
Correcting your code -
a = 12
b = 19.5
n = 10
i = a
while i < b:
x = i + 0.75 #pay attention here
print (i, x)
i += 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.