Python Generating Infinite Loop When First Arg is 0 -
i not understanding why below code falling infinite loop when 'start' argument of totalarea 0. have been watching code run in python tutor, , j executes through value of stop -1, resets 0 without moving onto value of stop.
import math def totalarea(start, stop, step): def f(x): return 10*math.e**(math.log(0.5)/5.27 * x) area = 0.0 j = start while j <= len(range(start, stop)): j in range(start, stop): area += float(step) * f(j) print area totalarea(0, 11, 1)
while j <= len(range(start, stop)): j in range(start, stop): every time loop ends, j = 10
len(range(start, stop)) 11
so j <= len(range(start, stop)) forever
the while loop seems entirely superfluous. try removing entirely:
def totalarea(start, stop, step): def f(x): return 10*math.e**(math.log(0.5)/5.27 * x) area = 0.0 j = start j in range(start, stop): area += float(step) * f(j) print area
Comments
Post a Comment